umair mohsin

umair mohsin

  • 1.4k
  • 387
  • 65.6k

record is not updating

Feb 13 2015 3:25 AM
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.

Attachment: demo gridview.rar

Answers (9)