Search code examples
c#sqldataadapterupdatecommand

C# SqlDataAdapter Must declare the scalar variable Sql Exception


New to C# and working on a Windows Form application. I am attempting to execute an update query against a SQL database, but keep running into "Must declare the scalar variable" error and I do not understand why.

The below code successfully opens the connection. My update statement is valid. Looking through a lot of posts on this topic and I am just not seeing my error... any help would be appreciated.

public void SetJobStatus(long JobId)
{
    string strSql = "update Jobmaster set jobstatus = 5 where equid = @stationId AND ID <> @jobId AND OfflineEntry = 0;";

    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = GlobalVars.connString;
        conn.Open();
        // use the connection here, and check to confirm it is open
        if (conn.State != ConnectionState.Open)
        {
            if (conn != null)
            {
                conn.Close();
            }
            conn.Open();
        }
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();

        command = new SqlCommand(strSql, conn);
        //below AddWithValue gives error:
        //System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@stationId".'
        //command.Parameters.AddWithValue("@stationId", 1);
        //command.Parameters.AddWithValue("@jobId", JobId);
        
        //next I tried this, and the same error:
        //System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@stationId".'
        command.Parameters.Add("@stationId", SqlDbType.Int);
        command.Parameters["@stationId"].Value = 1;
        command.Parameters.Add("@jobId", SqlDbType.Int);
        command.Parameters["@jobId"].Value = JobId;

        adapter.UpdateCommand = new SqlCommand(strSql, conn);
        adapter.UpdateCommand.ExecuteNonQuery();
    }
}

Solution

  • Thanks to everyone who chimed in here. WSC's comment did the trick- changing adapter.UpdateCommand = command; worked. I tried three variations of adding parameters after making WSC's change- two of them worked, one did not.

    My revised code is below. I have all three variations listed in the code- hopefully this will help somebody else out.

    public void SetJobStatus(long JobId)
    {
        string strSql = "update Jobmaster set jobstatus = 5 where equid = @stationId AND ID <> @jobId AND OfflineEntry = 0;";
    
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = GlobalVars.connString;
            conn.Open();
            // use the connection here, and check to confirm it is open
            if (conn.State != ConnectionState.Open)
            {
                if (conn != null)
                {
                    conn.Close();
                }
                conn.Open();
            }
            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();
    
            command = new SqlCommand(strSql, conn);
            
            //works
            command.Parameters.AddWithValue("@stationId", GlobalVars.stationId);
            command.Parameters.AddWithValue("@jobId", JobId);
    
            //works
            //command.Parameters.Add("@stationId", SqlDbType.Int);
            //command.Parameters["@stationId"].Value = 5;
            //command.Parameters.Add("@jobId", SqlDbType.Int);
            //command.Parameters["@jobId"].Value = JobId;
    
            //throws error at adapter.UpdateCommand.ExecuteNonQuery line:
            //'The parameterized query '(@stationId int,@jobId int)update Jobmaster set jobstatus = 5 wh' expects the parameter '@stationId', which was not supplied.'
            //command.Parameters.Add("@stationId", SqlDbType.Int, 5);
            //command.Parameters.Add("@jobId", SqlDbType.Int, (int)JobId);
    
            adapter.UpdateCommand = command;
            adapter.UpdateCommand.ExecuteNonQuery();
        }
    
    }