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

I am trying to update a certain record in my Student table, however the same exception keeps popping up as such:

System.Data.SqlClient.SqlException: Must declare the scalar variable “@sid”

Please do give any suggestions as I can’t seem to find any error with my code.

This is the code for my update query:

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you missed out @sid param

    if (conn.State == System.Data.ConnectionState.Closed)
    {
        conn.Open();

        String sid = txtSearch.Text;
        SqlCommand cmd2 = new SqlCommand("UPDATE [Student] SET [Password]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a39ee3d3c2d0d0">[email protected]</a>
            , [StudentName]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f522f1c1b1a0b210e020a">[email protected]</a>
            , [Email]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="526f12373f333b3e">[email protected]</a>
            , [PhoneNumber]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="af92efc1dac2cdcadd">[email protected]</a>
            , [Faculty]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c611c3a3d3f29302825">[email protected]</a>
            , [Course]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cb1ccefe3f9feffe9">[email protected]</a> 
        WHERE [StudentID]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85b8c5f6ece1">[email protected]</a>", 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();
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x