Why is my SQL CLR stored proc call claiming I am not providing a parameter I am?

StackOverflow https://stackoverflow.com/questions/22970303

  •  30-06-2023
  •  | 
  •  

Question

In a CLR procedure I have the following method:

private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string storedProcedureName = @"up_RemoveAssetOwnershipFromTeam";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(storedProcedureName, connection))
    {
        command.Parameters.AddWithValue("assetId", assetId);
        command.Parameters.AddWithValue("teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

When I run this method is called I am getting the following error:

Msg 6522, Level 16, State 1, Procedure cp_RemoveAsset, Line 0

A .NET Framework error occurred during execution of user-defined routine or aggregate "cp_RemoveAsset":

System.Data.SqlClient.SqlException: Procedure or function 'up_RemoveAssetOwnershipFromTeam' expects parameter '@assetId', which was not supplied.

System.Data.SqlClient.SqlException:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

at System.Data.SqlClient.SqlCommand.RunExecuteNonQuerySmi(Boolean sendToPipe)

at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)

at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

at StoredProcedures.EndOwnershipForTeam(Int64 assetId, Int32 teamId)

at StoredProcedures.cp_RemoveAsset(SqlInt32 userId, SqlString xaid)

Since my code is supplying the parameters (verified by displaying output via SqlContext.Pipe.Send() calls) why is it claiming that I"m not supplying a parameter I actually am?

Was it helpful?

Solution

Looks like as written, your code is instructing SQL Server to just try executing this:

up_RemoveAssetOwnershipFromTeam

In other words, just the procedure with no parameters supplied.

To wire up the parameters you need to either specify CommandType.StoredProcedure, or wire up the command parameters explicitly:

// option 1
private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string storedProcedureName = @"up_RemoveAssetOwnershipFromTeam";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(storedProcedureName, connection))
    {
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("assetId", assetId);
        command.Parameters.AddWithValue("teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

// option 2
private static void EndOwnershipForTeam(long assetId, int teamId)
{
    const string sql = @"exec up_RemoveAssetOwnershipFromTeam @assetId, @teamId";

    using (var connection = new SqlConnection("context connection=true"))
    using (var command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@assetId", assetId);
        command.Parameters.AddWithValue("@teamId", teamId);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

OTHER TIPS

You left out the @ symbols in your lines

command.Parameters.AddWithValue("assetId", assetId);
command.Parameters.AddWithValue("teamId", teamId);

They should be

command.Parameters.AddWithValue("@assetId", assetId);
command.Parameters.AddWithValue("@teamId", teamId);
  1. You've omitted the '@' prefix.
  2. What is the default collation configured for your sql server and/or database? If it is case-sensitive, you'll need to match case exactly.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top