Thursday, February 22, 2007

Editing & Updating The Data Through Data Grid

This The simple program to edit the Data and store them in The DataBase Using DataGrid Control

DG1 Is Id For DataGrid Control,To Get The CancelCommand,EditCommand,UpdateCommand,
in the Property Windows Click   this button the Event, then select the above mentioned event.




main thing is dont forget to add this if(!IsPostBack) line,else each time event rised the page will be refreshed and new data will not be updated, old data only updated in the database

Codeing
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DyGrid
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DG1;
SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=databasename;uid=username;
pwd=password");
DataSet ds= new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
DBCon();
}

public void DBCon()
{
SqlDataAdapter da =new SqlDataAdapter("Select * From Emp",con);
da.Fill(ds,"EmpTable");
DG1.DataSource=ds.Tables[0];
DG1.DataBind();
DG1.DataKeyField = "iEid";
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.DG1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_CancelCommand);

this.DG1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_EditCommand_1);

this.DG1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_UpdateCommand_1);

this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DG1_UpdateCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string s1,s2,s3;
TextBox Eid = (TextBox)e.Item.Cells[1].Controls[0];
s1=Eid.Text;
TextBox Ename=(TextBox)e.Item.Cells[2].Controls[0];
s2=Ename.Text;
TextBox Esal=(TextBox)e.Item.Cells[3].Controls[0];
s3=Esal.Text;
SqlCommand cmd =new SqlCommand("Update Emp Set dSalary=" + s3+ " ,cEName ='" + s2 + "' WHERE iEid=" + Convert.ToInt32(s1),con);
con.Open();
cmd.ExecuteNonQuery();
DG1.EditItemIndex=-1;
DBCon();
con.Close();
}

private void DG1_EditCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DG1.EditItemIndex = e.Item.ItemIndex;
DBCon();
}

private void DG1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DG1.EditItemIndex=-1;
DBCon();
}
}
}

Output

Intial

After UpdateLink Button Triggered(Clicked)


Data Which Is To Be Updated


Updated Content

2 comments:

Unknown said...

it is very nice .easy to understand the code

Unknown said...

it is very nice .easy to understand the code