Record is not updating when I click to update button. my database table has four columns id is set to primary key and other columns are first name last name and class
Here is my code for row updating event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
cs=connection string in web.config file.
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = " update student_record set firstname=@firstname,lastname=@lastname,class=@class where id=@id";
cmd.CommandType = CommandType.Text;
string id=GridView1.Rows[e.RowIndex ].Cells [2].Text ;
string fname=((TextBox)GridView1.Rows[e.RowIndex ].FindControl("TextBox1")).Text ;
string lname=((TextBox )GridView1.Rows [e.RowIndex ].FindControl("TextBox2")).Text ;
string clas = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
conn.Open();
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@firstname", fname);
cmd.Parameters.AddWithValue("@lastname", lname );
cmd.Parameters.AddWithValue("@class", clas);
cmd.ExecuteNonQuery();
}
GridView1.EditIndex = -1;
binddata();
here is the bind data function coding
private void binddata()
{
SqlConnection conn = new SqlConnection(cs);
DataSet ds = new DataSet();
string select = "select * from student_record";
SqlDataAdapter sda = new SqlDataAdapter(select ,conn );
conn.Open();
sda.Fill(ds, "mydbtable1");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
source file is also attached.anyone help me in this.
Loading

Farhad Mirshekar13 reputationPosted Feb 13, 2015, 7:42 PM EST
For example, Your template Grid should look as follows:
Edit the code is as follows:
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label id = GridView2.Rows[e.RowIndex].FindControl("Lblid") as Label;
TextBox name = GridView2.Rows[e.RowIndex].FindControl("txtname") as TextBox;
TextBox city = GridView2.Rows[e.RowIndex].FindControl("txtcity") as TextBox;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand com = new SqlCommand("Update tbl_Employee set Name=@name,City=@city Where ID=@id", con);
com.Parameters.AddWithValue("@id", SqlDbType.Int).Value = Convert.ToInt32(id.Text);
com.Parameters.AddWithValue("@name", SqlDbType.NVarChar).Value = name.Text;
com.Parameters.AddWithValue("@city", SqlDbType.NVarChar).Value = city.Text;
com.ExecuteNonQuery();
con.Close();
GridView2.EditIndex = -1;
BindGrid();
Response.Write("OK");
}
Pradeep ShetRank #595 · 4K reputationPosted Feb 12, 2015, 11:27 PM EST
string id=GridView1.Rows[e.RowIndex ].Cells [2].Text ;
umair mohsinRank #3,092 · 463 reputationPosted Feb 12, 2015, 11:33 PM EST
is there any issue with method i used.
cmd.parameters.addwith value?.guide me please
Munesh SharmaRank #174 · 13.9K reputationPosted Feb 13, 2015, 12:10 AM EST
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html
VulpesRank #55 · 29.2K reputationPosted Feb 13, 2015, 12:20 AM EST
If it is, try:
cmd.Parameters.AddWithValue("@id", int.Parse(id));
umair mohsinRank #3,092 · 463 reputationPosted Feb 13, 2015, 3:53 AM EST
umair mohsinRank #3,092 · 463 reputationPosted Feb 13, 2015, 8:09 PM EST
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 16, 2023, 6:31 AM EDT
Try This
command.Parameters.Add("@ID", SqlDbType.Int); c
ommand.Parameters["@ID"].Value = customerID;
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 10:33 AM EDT
I think you are missing the DataKeyNames attribute in the GridView declaration in your ASPX file. Without specifying the DataKeyNames, the GridView doesn't know which column is the primary key, so it cannot properly update the row.