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 MirshekarPosted Feb 14, 2015, 12:42 AM
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");
}
Tuhin PaulPosted Mar 16, 2023, 2:33 PM
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.
Rajeev KumarPosted Mar 16, 2023, 10:31 AM
Try This
command.Parameters.Add("@ID", SqlDbType.Int); c
ommand.Parameters["@ID"].Value = customerID;
umair mohsinPosted Feb 14, 2015, 1:09 AM
umair mohsinPosted Feb 13, 2015, 8:53 AM
VulpesPosted Feb 13, 2015, 5:20 AM
If it is, try:
cmd.Parameters.AddWithValue("@id", int.Parse(id));
Munesh SharmaPosted Feb 13, 2015, 5:10 AM
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html
umair mohsinPosted Feb 13, 2015, 4:33 AM
is there any issue with method i used.
cmd.parameters.addwith value?.guide me please
Pradeep ShetPosted Feb 13, 2015, 4:27 AM
string id=GridView1.Rows[e.RowIndex ].Cells [2].Text ;