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

Update in ASP.NET results in an exception: "must declare scalar variable"

我正在尝试更新我的 Student table 中的某条记录,但是同样的异常不断弹出:

System.Data.SqlClient.SqlException: Must declare the scalar variable "@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 参数

    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();
    }