更新群组时出错。必须声明标量变量“ @SubnameWHERE” ...为什么显示此错误.....
protected void cmdUpdate_Click(object sender, EventArgs e)
{
string updateSQL;
updateSQL = "UPDATE tblnewgroup SET ";
updateSQL += "Groupname=@Groupname, ";
updateSQL += "Slno=@Slno, Subname=@Subname";
updateSQL += "WHERE Groupno=@Groupno_original";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(updateSQL, con);
cmd.Parameters.AddWithValue("@Groupname", txtgname.Text);
cmd.Parameters.AddWithValue("@Slno", txtsl.Text);
cmd.Parameters.AddWithValue("@Subname", txtsn.Text);
cmd.Parameters.AddWithValue("@Groupno_original", lstAuthor.SelectedItem.Value);
int updated = 0;
try
{
con.Open();
updated = cmd.ExecuteNonQuery();
lblResults.Text = updated.ToString() + " record updated.";
}
catch (Exception err)
{
lblResults.Text = "Error updating Group. ";
lblResults.Text += err.Message;
}
finally
{
con.Close();
}
if (updated > 0)
{
FillAuthorList();
}
}
最佳答案
Subname=@Subname
后需要一个空格
改变你的
updateSQL += "Slno=@Slno, Subname=@Subname";
至
updateSQL += "Slno=@Slno, Subname=@Subname ";
为了更好地可视化,您可以像这样使用它;
string updateSQL = @"UPDATE tblnewgroup
SET Groupname=@Groupname, Slno=@Slno, Subname=@Subname
WHERE Groupno=@Groupno_original";
同样,您应该使用
using
块来处理SqlConnection
;using(SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
updated = cmd.ExecuteNonQuery();
...
}