Friday, January 4, 2008

Edit, Delete, Update and Insert operations in a DataGrid

The Source code for this is: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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection con;
SqlCommand cmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Binddata();
}
}
// Define the Edit Command
public void editgrid_click(object sender, DataGridCommandEventArgs e)
{
gridedit.EditItemIndex = e.Item.ItemIndex;
Binddata();
}
// Define the Cancel Command
public void gridcancel_click(object sender, DataGridCommandEventArgs e)
{
gridedit.EditItemIndex = -1;
Binddata();
}
//Here we Bind the data
public void Binddata()
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText="select * from record order by id Asc";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
gridedit.DataSource = ds;
gridedit.DataBind();
con.Close();
}
//Update Command Defination
protected void updategrid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
try
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText = "Update record set name=@name ,F_name=@F_Name, l_name=@l_name,City=@City,State=@State where id=@id";
cmd.Parameters.Add("@name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
cmd.Parameters.Add("@F_name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@l_name", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@City", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@State", SqlDbType.Char).Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = gridedit.DataKeys[e.Item.ItemIndex];
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
// Delete Command Defination
public void gridedit_DeleteCommand(object sender, DataGridCommandEventArgs e)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
int U_ID = (int)gridedit.DataKeys[(int)e.Item.ItemIndex];
cmd.CommandText = " Delete from record where id=" + U_ID;
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
}
// For Paging
public void gridedit_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
gridedit.CurrentPageIndex = e.NewPageIndex;
Binddata();
}
//Link for Insert a new Record in a table
public void InsertNewRecord_click(object source, System.EventArgs e)
{
lblnewname.Visible = true;
txtnewname.Visible = true;
lblF_name.Visible = true;
txtF_name.Visible = true;
lblLast_name.Visible = true;
txtLast_Nmae.Visible = true;
lblcity.Visible = true;
txtcity.Visible = true;
lblState.Visible = true;
txtState.Visible = true;
btnnewRecordSubmit.Visible = true;
}
// Command for insert a new Record
public void Submitnew(object source, System.EventArgs e)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
cmd.CommandText = "insert into record(name,F_name,l_name,City,State) values(@name,@F_Name,@l_name,@City,@State)";
cmd.Parameters.Add("@name", SqlDbType.Char).Value = txtnewname.Text;
cmd.Parameters.Add("@F_Name", SqlDbType.Char).Value = txtF_name.Text;
cmd.Parameters.Add("@l_name", SqlDbType.Char).Value = txtLast_Nmae.Text;
cmd.Parameters.Add("@City", SqlDbType.Char).Value = txtcity.Text;
cmd.Parameters.Add("@State", SqlDbType.Char).Value = txtState.Text;
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
gridedit.EditItemIndex = -1;
Binddata();
txtnewname.Text = "";
txtF_name.Text = "";
txtLast_Nmae.Text = "";
txtcity.Text = "";
txtState.Text = "";
lblnewname.Visible = false;
txtnewname.Visible = false;
lblF_name.Visible = false;
txtF_name.Visible = false;
lblLast_name.Visible = false;
txtLast_Nmae.Visible = false;
lblcity.Visible = false;
txtcity.Visible = false;
lblState.Visible = false;
txtState.Visible = false;
btnnewRecordSubmit.Visible = false;
}}Now when User will run the project then the window will look like as:

No comments: