Here is my code:
Asp:
c#:
public partial class Admin_UserRole : System.Web.UI.Page
{
SqlConnection Con;
SqlCommand Cmd;
SqlDataAdapter Sdap;
protected void Page_Load(object sender, EventArgs e)
{
Con = new SqlConnection(@"Data Source=SMAART-1F1D3825\SQLEXPRESS;Initial Catalog=SundarTyres;Integrated Security=True");
if (!IsPostBack)
{
gvbind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Add")
{
TextBox txtaddbranch = (TextBox)GridView1.FooterRow.FindControl("txtaddbranch");
TextBox txtaddusername = (TextBox)GridView1.FooterRow.FindControl("txtaddusername");
TextBox txtaddpassword = (TextBox)GridView1.FooterRow.FindControl("txtaddpassword");
Con.Open();
Cmd = new SqlCommand("Insert Into Admin_Login Values('"+txtaddbranch.Text+"','"+txtaddusername.Text+"','"+txtaddpassword.Text+"')", Con);
Cmd.ExecuteNonQuery();
}
}
catch
{
}
finally
{
Con.Close();
gvbind();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
Label id = (Label)row.FindControl("lblid");
int TID = Convert.ToInt32(id.Text);
Con.Open();
TextBox Branch = (TextBox)row.FindControl("TextBox1");
TextBox Name = (TextBox)row.FindControl("TextBox2");
TextBox Password = (TextBox)row.FindControl("TextBox3");
Cmd = new SqlCommand("Update Admin_Login Set Branch='" + Branch.Text + "',Name='" + Name.Text + "',Password='" + Password.Text + "' Where T_ID=" + TID + "", Con);
Cmd.ExecuteNonQuery();
Con.Close();
GridView1.EditIndex = -1;
gvbind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
Label lblid = (Label)row.FindControl("lblid");
int T_ID = Convert.ToInt32(lblid.Text);
Con.Open();
Cmd = new SqlCommand("Delete From Admin_Login Where T_ID="+T_ID+"",Con);
Cmd.ExecuteNonQuery();
Con.Close();
GridView1.EditIndex = -1;
gvbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}
public void gvbind()
{
try
{
Con.Open();
Sdap = new SqlDataAdapter("Select * From Admin_Login", Con);
DataSet ds = new DataSet();
Sdap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch
{
}
finally
{
Con.Close();
}
}
}

Priti KumariPosted Jan 27, 2014, 6:15 AM
http://www.codeproject.com/Articles/667599/Add-Edit-update-Delete-gridview
http://www.codeproject.com/Questions/407859/Edit-update-and-delete-from-grid-view-in-asp-net
http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/
selva kumarPosted Jan 27, 2014, 5:21 AM
Priti KumariPosted Jan 27, 2014, 4:46 AM
And you can see attachment i have to change Version compatibility.....
Please don't forget to mark as answer if this post helps you
selva kumarPosted Jan 27, 2014, 4:35 AM
Priti KumariPosted Jan 27, 2014, 4:21 AM
Please don't forget to mark as answer if this post helps you.