Values are not updating

89 views Asked by At
protected void gvExample_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   string s = gvExample.DataKeys[e.RowIndex].Value.ToString();           
   TextBox FirstName = gvExample.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
   TextBox LastName = gvExample.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
   TextBox Title = gvExample.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;
   TextBox Country = gvExample.Rows[e.RowIndex].FindControl("txtCountry") as TextBox;
   string query ="UPDATE USER_DETAILS SET LAST_NAME='" + LastName.Text + "',FIRST_NAME='" + FirstName.Text + "',TITle='" + Title.Text + "',COUNTRY='" + Country.Text + "'";

   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);
   SqlCommand cmd = new SqlCommand(query, con);
   con.Open();
   cmd.ExecuteNonQuery();
   con.Close();
   gvDatabind();
}

The values are not updating. They are updating as lastname.Text firstname.Text. Please help me.

1

There are 1 answers

2
Simon Martin On

It sounds like your variables FirstName etc are not being cast as TextBox properly... What if you try something like the following cast

GridViewRow row = gvExample.Rows[e.RowIndex];
var Id = ((TextBox)(row.Cells[0].Controls[0])).Text;
var FirstName = ((TextBox)(row.Cells[1].Controls[0])).Text;
var LastName = ((TextBox)(row.Cells[2].Controls[0])).Text;

string query ="UPDATE USER_DETAILS SET LAST_NAME='" + LastName + "',FIRST_NAME='" + FirstName "'"; // and your other values...

Obviously also including a WHERE clause and int.parse the Id value for that and generally making the example code you've shown more production ready.