简体   繁体   中英

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

I have checked your code and it's required some changes. Please try to run below code:

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

    using (SqlConnection conn = new SqlConnection())
    {
        try
        {
            conn.ConnectionString = GlobalVars.connString;
            conn.Open();
            SqlCommand command = new SqlCommand(strSql, conn);
            command.CommandType = CommandType.Text;
            command.Parameters.Add("@stationId", SqlDbType.Int);
            command.Parameters["@stationId"].Value = 1;
            command.Parameters.Add("@jobId", SqlDbType.Int);
            command.Parameters["@jobId"].Value = JobId;
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
}

Tips:

  • Always close connection after completion of task or in case of error.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM