Click here to Skip to main content
16,017,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    int BookingRefNo = int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
    string AssignedPriority = ((TextBox)row.Cells[11].Controls[0]).Text;
    string Status = ((TextBox)row.Cells[12].Controls[0]).Text;
    SqlConnection con1 = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("Update TSafetyMaster set AssignedPriority=@AssignedPriority,Status=@Status where BookingRefNo=@BookingRefNo", con1);
    cmd.Parameters.Add("@BookingRefNo", SqlDbType.Int).Value = BookingRefNo;
    cmd.Parameters.Add("@AssignedPriority", SqlDbType.VarChar).Value = AssignedPriority;
    cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = Status;
    con1.Open();
    cmd.ExecuteNonQuery();
    GridView2.EditIndex = -1;
    GridView2.DataBind();
}
Posted
Updated 16-May-14 20:39pm
v2
Comments
DamithSL 17-May-14 2:34am    
how you load the load data first time?

you need to get the data from database and bind the GridView2 again after update.
for example, in your page load you may have gridview binding code something like below

C#
if(!IsPagePostBack)
{
    BindGridview(); // get data from database and call GridView2.DataBind();
}


call the same after insert
C#
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  // your current code goes here ...
  // after cmd.ExecuteNonQuery();
   BindGridview();
}
 
Share this answer
 
v2
The problem is with the way you are binding the Grid. You must check for IsPostBack in Page Load and bind the data.
Otherwise, for RowUpdating, it would go inside the Page Load first and bind the Grid again. Do, you will get the old values instead of new values.

So, do like below...
C#
private void Page_Load()
{
    if (!IsPostBack)
    {
        // Call your Bind method here.
        BindYourGrid();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900