Wednesday, December 23, 2009

Creating custom Server Control

write this coding in Web Control Library Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace my_first_control
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,INamingContainer
{
protected TextBox new_textbox = new TextBox();
protected Button button = new Button();

The Below Strings are Displayed Default in a cs file.Am attached the meaning for the terms below....

[Bindable(true)]----specifies for visual designers whether it is meaningful to bind the property to data

[Category("Appearance")]----specifies how to categorize the property in the visual designer's property browser

[DefaultValue("")]----specifies a default value for the property

[Localizable(true)]----specified as true or false, specifies for visual designers whether it is meaningful to localize the property.

public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();//EnsureChildControls method checks if CreateChildControls is already called, and call it if not
return base.Controls;
}
}
protected override void CreateChildControls()
{
//base.CreateChildControls();

Controls.Clear();
new_textbox.ToolTip = "User Created Control";
new_textbox.Text = "NEW CREATED TEXT BOX";

button.Click += new EventHandler(button_Click);

Controls.Add(new_textbox);
Controls.Add(new LiteralControl("
"));
Controls.Add(button);
Controls.Add(new LiteralControl("
"));
}
private void button_Click(object sender, EventArgs e)
{
EnsureChildControls();
new_textbox.Text = "TEXT WORKS";
}
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderControl(writer);
}

protected override void OnInit(EventArgs e)
{
string javascript_code_data = @"";
Page.RegisterClientScriptBlock("my script", javascript_code_data);
base.OnInit(e);
}
}
}

Build the Form as Dll.It Stores in Project Foldrename-->Debug-->Bin-->Filename.dll
add the dll reference to ur page.

We have Two type to Register the Control into our Web From
1. Using Register Namesapace="my_frist_Control" Assembly="My_First_Control" TagPrefix="vijay" with in the tag "<%@Register %>"

2. Add this web Config File.If u Register in web Config File.You need not to Register in Each File.You can use the control in Different File.
system.web
pages
controls
add tagPrefix="mycontrol" namespace="my_first_control" assembly="my_first_control"/
/controls
/pages
/system.web

Then Call the tag name in Web form as

mycontrol:WebCustomControl1 ID="my_control" runat="server"

It Will Show your Control on your Web Form.

Monday, September 14, 2009

Query for creating store procedure for running dynamic table with single Procedure

create procedure select_table_dynamic @table_name nvarchar(1000) as
Declare @table nVarchar(1000)
set @table = 'select * from ' + @table_name
exec(@table)

Run Store Procedure

Exec  select_table_dynamic 'table_name'

Monday, September 7, 2009

Coding for Export Sqlserver Data into Excel

Before go in for export,just configure ur Database Driver With the Given Code Below.

EXEC sp_configure 'show advanced options', 1;

GO
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO

While Run a below code U must create a xls file in a give path,and specify the header in that file.

Below Code is used to Export Sql Data into Excel Format


INSERT INTO
  OPENROWSET( 'Microsoft.Jet.OLEDB.4.0',
  'Excel 8.0;Database=C:\testing.xls',
  'select fname, lname, email, dateadd, lastchgd, firsttimeuser, active from sql4.ctsi.dbo.users where custno=0913 and active=Y
 FROM [Sheet1$]')

  select fname, lname, email, dateadd, lastchgd, firsttimeuser, active from sql4.ctsi.dbo.users where custno='0913' and active='Y'

Monday, August 31, 2009

Coding for creating chart using c#.net

Add references of  Microsoft office components from com tab.

Include the header file

Using System.OWC;

  //Create chartspace object to hola a chart
  OWC.ChartSpace draw_chart = new OWC.ChartSpace();

  //Add a chart in chartspace
  OWC.WCChart chart_obj = draw_chart.Charts.Add(0);

  //specify the type of chart to be displayed
  chart_obj.Type = OWC.ChartChartTypeEnum.chChartTypeColumnClustered;

  //specify the chart legend value,if it is needed.
  chart_obj.HasLegend = true;

  //set the chart caption on or off and set the caption value of chart.
  chart_obj.HasTitle = true;
  chart_obj.Title.Caption = "Demo Chart";

  //set the caption of x and y axis positions
  chart_obj.Axes[0].HasTitle = true;
  chart_obj.Axes[0].Title.Caption = "POSITION X";
  chart_obj.Axes[1].HasTitle = true;
  chart_obj.Axes[1].Title.Caption = "POSITION Y";

  //creating a values for a chart 
  String bar_name = "Line 1";
  string x_values = "1" + '\t' + "2" + '\t' + "3";
  string y_values = "1" + '\t' + "2" + '\t' + "3";

  //add the seriescollection in chart chart object
  chart_obj.SeriesCollection.Add(0);

  //Binding the chart values in seriescollection array.
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimSeriesNames, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, bar_name);
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimCategories, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, x_values);
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimValues, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, y_values);

  //specify the chart export path.
  string chart_path = "chart.gif";
  draw_chart.ExportPicture(chart_path, "gif", 500, 500);

  //chart file name to be displayed
  string gif_file_path = "chart.gif";

  //binding the filename into a image tag.
  string img_tag="include image tag here with file source name of chart file name mentioned above";

  //Add a imagetage into a placedholder to view the chart
  chart_container.Controls.Add(new LiteralControl(img_tag));

Friday, August 21, 2009

Stored Procedure for login count and update the count in a table

alter procedure login_check_value (@username varchar(15),@pass_word varchar(15))
as
declare @result int
begin
if(EXISTS(select * from table_check where username=@username and pass_word=@pass_word))
begin
Set @result=(select access from table_check where username=@username and pass_word=@pass_word)
set @result=@result+1
update table_check set access=@result where username=@username and pass_word=@pass_word
end
else
begin
set @result=0
update table_check set access=@result where username=@username and pass_word=@pass_word
end
begin
select * from table_check
end
end

Thursday, July 30, 2009

Create New Xml File And Update values of the attributs in that XMl File

Creating New XmlFile

XmlTextWriter xml_writer = new XmlTextWriter(Server.MapPath(path), null);
  xml_writer.WriteStartDocument();
  xml_writer.WriteRaw("");
  xml_writer.WriteStartElement("Login_Details");
  xml_writer.WriteEndElement();
  xml_writer.WriteEndDocument();
  xml_writer.Close();


For Updating Attributes Value OF Xml 

XmlDocument xml_Doc = new XmlDocument();

xml_Doc.Load(Server.MapPath(path));

   XmlNode xml_node = xml_Doc.SelectSingleNode("Login_Details");
   foreach (XmlNode node in xml_node)
  {
    if (node.Attributes[0].InnerText.ToString()=="Test")
  {
  node.Attributes[0].InnerText = "Text_Modified";
  }
  }
  xml_Doc.Save(Server.MapPath(path));

Coding for Checking User Authentication with XML and Create login Details as XML File

XML Files


Create Xml File then use the below code to access.

C# Codings

int i, j;

   
  string users_pass = string.Empty;
  string users_fail = string.Empty;
   
  XmlDocument xml_document = new XmlDocument();
  xml_document.Load(Server.MapPath("Login_Details.xml"));
  XmlNodeList node_list = xml_document.GetElementsByTagName("username");
  for ( i = 0; i < j =" 0;" user_name="node_list[i].ChildNodes[0].InnerText;" pass_word="node_list[i].ChildNodes[1].InnerText;" pass_count =" pass_count" pass_username =" user_name.ToString();" fail_count =" fail_count">= 1)
  {
   string date = System.DateTime.Now.ToShortDateString();
  string date_format = date.Replace('/', '-');
  string path = "Login_info_" + date_format.ToString() + ".XML";
  XmlDocument xml_doc = new XmlDocument();
  xml_doc.Load(Server.MapPath(path));
  XmlNode xml_sel = xml_doc.SelectSingleNode("Login_Details");
  XmlElement xml_ele = xml_doc.CreateElement("Login_Info");
  xml_ele.SetAttribute("user_login", pass_username.ToString());
  xml_sel.AppendChild(xml_ele);
   xml_ele.SetAttribute("login_time", System.DateTime.Now.ToString());
  xml_sel.AppendChild(xml_ele);
  xml_doc.Save(Server.MapPath(path));
  Response.Write("System pass :"+pass_username.ToString());
  pass_count = 0;
  }
  else
  {
  Response.Write("System Fails :"+pass_username.ToString());
  fail_count = 0;
  }

Wednesday, July 22, 2009

Creating Delegates Using C#.net

Creating Of Deletecates Events

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace delegates
{
  ///


  /// Summary description for Delegates
  ///

  public class Delegates : System.Collections.ArrayList
  {
  public event System.EventHandler changed;

  protected virtual void Onchanged(System.EventArgs e)
  {
  if (changed != null)
  {
  changed(this, e);
  }
  }
  public override int Add(object value)
  {
  int i = base.Add(value);
  Onchanged(System.EventArgs.Empty);
  return i;

  }
  public Delegates()
  {

  //
  // TODO: Add constructor logic here
  //
  }
  }

}

Creation Of EventHander and Fucntions

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///


/// Summary description for delegates_list
///

/// 
using delegates;
namespace delegates_list_pro
{
public class delegates_list
{
  private Delegates list1;
  public delegates_list(Delegates alist)
  {
  list1=alist;
  list1.changed+=new System.EventHandler(listadded);
   
  }
  private void listadded(object sender,EventArgs e)
  {
  System.Windows.Forms.MessageBox.Show("WELCOME");
  }
  public void Detach()
  {
  list1.changed-=new System.EventHandler(listadded);
  list1=null;
  }



 public delegates_list()
 {
  //
  // TODO: Add constructor logic here
  //
 }
}
}

Calling Detlegates and its event handlers from the main program

using delegates;
using delegates_list_pro;

Delegates dele = new Delegates();
delegates_list listener = new delegates_list(dele);
dele.Add("Welcome");
dele.Clear();
listener.Detach();

Thursday, July 2, 2009

Coding for Creating a Window Snap Shot Using C#.net

Add the reference of system.windows.form by address reference option 

using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

//file name is created based on a time
File_name.Text = System.DateTime.Now.ToShortTimeString();
  string file_name = File_name.Text.Replace(':', '-');
  Bitmap image = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image);  graphics.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size, System.Drawing.CopyPixelOperation.SourceCopy);
 image.Save("C:/temp/" + file_name.ToString()+".jpg");
Note:
IF You want to take a snap shot for particualr timer interval, just added a meta tag inside head tag
meta http-equiv="refresh" content="60"
60 refers the page refreshing time in a seconds..