protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //更新行数据
{
string sql = "update spareParts set spname='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + " '";
sql += ",sptype='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + " '";
sql += ",spnumber='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + " '" ;
sql +=",spre='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + " '";
sql += ",where spNo='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + " '";
mydb.ExecuteNonQuery(sql);
GridView1.EditIndex = -1;
DataBind();
mydb.close();
mydb.Dispose();
}
出错:
在关键字 'where' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'where' 附近有语法错误。源错误:
行 114: catch (Exception exp)
行 115: {
行 116: throw exp;
行 117: }
行 118: finally
{
string sql = "update spareParts set spname='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + " '";
sql += ",sptype='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + " '";
sql += ",spnumber='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + " '" ;
sql +=",spre='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + " '";
sql += ",where spNo='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + " '";
mydb.ExecuteNonQuery(sql);
GridView1.EditIndex = -1;
DataBind();
mydb.close();
mydb.Dispose();
}
出错:
在关键字 'where' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'where' 附近有语法错误。源错误:
行 114: catch (Exception exp)
行 115: {
行 116: throw exp;
行 117: }
行 118: finally
mydb.ExecuteNonQuery(sql);
GridView1.EditIndex = -1;
DataBind();
mydb.close();
==
把where前面的逗号去掉就可以