这是Asp.net按钮代码
<asp:Button ID="Button2" runat="server" Text="Approved Job Request" CssClass="css_button" CommandName="ApplyButton" CommandArgument='<%#Eval("AId")%>' />
这是C#代码
if (e.CommandName == "ApplyButton")
{
int userId = int.Parse(e.CommandArgument.ToString());
SqlConnection con = new SqlConnection("Data Source=Mrunal;Initial Catalog=JobPortalDB;Integrated Security=True");
String sql = "update AppliedJob set AApprove='yes' where AId=userId";
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/ReqHome.aspx");
}
使用上面的代码,得到错误为
System.Data.SqlClient.SqlException:无效的列名'userId'
。该怎么办?
因为您在update子句中的条件不正确
您可以像这样:
String sql = "update AppliedJob set AApprove='yes' where AId='"+userId+"'";
但是以上功能无法用于SQL注入,我几乎不建议使用SQL参数,因此请尝试以下操作:
int userId = int.Parse(e.CommandArgument.ToString());
String sql = "update AppliedJob set AApprove='yes' where AId=@userId";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue("@userId", userId);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句