I'm able to populate a gridview using stored proc from code behind. But can't seem to figure out how to update it. In my rowupdating, I'm trying to get the value of a text box but it always picks up the old value. Please help. Here's my code:
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string labeltable = "Columns";
int KeyIn;
KeyIn = Convert.ToInt16(GridView2.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
TextBox notestxt = (TextBox)row.FindControl("unotesTextbox") as TextBox;
string n = string.Empty;
if (notestxt != null)
{
n = notestxt.Text;
}
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["xxxConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateNotes";
cmd.Connection = sqlcon;
sqlcon.Open();
try
{
cmd.Parameters.Add("@TableNmIn", SqlDbType.NVarChar).Value = labeltable;
cmd.Parameters.Add("@KeyIn", SqlDbType.Int).Value = KeyIn;
cmd.Parameters.Add("@NotesIn", SqlDbType.NVarChar).Value = n;
GridView2.EditIndex = -1;
GridView2.DataBind();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.SelectedIndex = -1;
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataBind();
}