[英]Error Updating SQL Server
I get an error when l try to update my table. 我尝试更新表时收到错误消息。 The table consist of 6 columns which l display in a data grid view on the windows form.
该表由6列组成,这些列在Windows窗体上以数据网格视图显示。 The ID column is displayed in a label on the form to prevent users from editing.
ID列显示在表单上的标签中,以防止用户进行编辑。
Now when l try to update I get this error: 现在,当我尝试更新时,出现此错误:
The multi-part identifier "lblId.Text" could not be bound.
多部分标识符“ lblId.Text”无法绑定。
Any suggestions will be helpful. 任何建议都会有所帮助。
Here is my update code: 这是我的更新代码:
private void btnupdate_Click(object sender, EventArgs e)
{
cn.Open();
cmd = new SqlCommand ("UPDATE jimmy SET Id=@Id,
FirstName=@FirstName, LastName=@LastName, Telephone=@Telephone,
Address=@Address, City=@City,Country=@Country WHERE @Id=lblId.Text", cn);
cmd.Parameters.AddWithValue("@Id", lblID.Text);
cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
cmd.Parameters.AddWithValue("@LastName", txtlastname.Text);
cmd.Parameters.AddWithValue("@Telephone", txttelephone.Text);
cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
cmd.Parameters.AddWithValue("@City", txtcity.Text);
cmd.Parameters.AddWithValue("@Country", txtcountry.Text);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Updated");
cleartext();
}
The error came from your SQL command text at the WHERE
clause. 该错误来自
WHERE
子句中的SQL命令文本。
It should be 它应该是
WHERE Id = @Id
Instead of 代替
WHERE @Id = lblId.Text
You can't say to SQL Server to read a text from your textbox controller by putting lblId.Text
directly into the SQL Command text. 您不能通过将
lblId.Text
直接放入SQL Command文本中来告诉SQL Server从文本框控制器中读取文本。
WHERE @Id=lblId.Text
应该在WHERE Id=@Id
因为lblId.Text
最有可能是您要作为参数值传递的UI控件值,无论如何您都说cmd.Parameters.AddWithValue("@Id", lblID.Text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.