Reputation: 27803
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?
Upvotes: 0
Views: 182
Reputation: 74227
Upvotes: 0
Reputation: 6850
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();
}
}
Upvotes: 3
Reputation: 511
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);
Upvotes: 3