值未更新

时间:2014-03-18 12:50:22

标签: asp.net datagridview webforms

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();
}

值未更新。它们正在更新为lastname.Text firstname.Text。 请帮帮我。

1 个答案:

答案 0 :(得分:2)

听起来你的变量FirstName等没有被正确地投射为TextBox ...如果你尝试类似下面的演员怎么办

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...

显然还包括一个WHERE子句和int.parse Id的值,并且通常会使示例代码显示更多的生产准备就绪。