ASP.NET 中的更新会导致异常:"must declare scalar variable"



我正在尝试更新我的Student表中的某个记录,但是相同的异常不断弹出如下:

System.Data.SqlClient。必须声明标量变量"@sid">

请给我一些建议,因为我的代码似乎找不到任何错误。

这是我的更新查询代码:

if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
String sid = txtSearch.Text;
SqlCommand cmd2 = new SqlCommand("UPDATE [Student] SET [Password] = @pass, [StudentName] = @studName, [Email] = @email, [PhoneNumber] = @number, [Faculty] = @faculty, [Course] = @course WHERE [StudentID] = @sid", conn); 
cmd2.Parameters.AddWithValue("@pass", txtPassword.Text);
cmd2.Parameters.AddWithValue("@studName", txtStudentName.Text);
cmd2.Parameters.AddWithValue("@email", txtEmail.Text);
cmd2.Parameters.AddWithValue("@number", txtPhoneNum.Text);
cmd2.Parameters.AddWithValue("@faculty", ddlFaculty.SelectedValue);
cmd2.Parameters.AddWithValue("@course", ddlCourse.SelectedValue);
cmd2.ExecuteNonQuery(); 
Response.Write("<script>alert('Record updated successfully!')</script>");
conn.Close();
Clear();
}

你错过了@sid param

if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
String sid = txtSearch.Text;
SqlCommand cmd2 = new SqlCommand("UPDATE [Student] SET [Password]=@pass
, [StudentName]=@studName
, [Email]=@email
, [PhoneNumber]=@number
, [Faculty]=@faculty
, [Course]=@course 
WHERE [StudentID]=@sid", conn); 
cmd2.Parameters.AddWithValue("@pass", txtPassword.Text);
cmd2.Parameters.AddWithValue("@studName", txtStudentName.Text);
cmd2.Parameters.AddWithValue("@email", txtEmail.Text);
cmd2.Parameters.AddWithValue("@number", txtPhoneNum.Text);
cmd2.Parameters.AddWithValue("@faculty", ddlFaculty.SelectedValue);
cmd2.Parameters.AddWithValue("@course", ddlCourse.SelectedValue);
cmd2.Parameters.AddWithValue("@isid", sid);     // <------
cmd2.ExecuteNonQuery(); 
Response.Write("<script>alert('Record updated successfully!')</script>");
conn.Close();
Clear();
}

相关内容

  • 没有找到相关文章

最新更新