In general we are supposed to pass integer values to our stored procedures and to do so we usually do it with this method
command.Parameters.AddWithValue("@param1", paramValue);
But, I found it very strange that if we need to pass uint datatype parameter to the stored procedure using above method, it gives a strange exception. Although its not when the code hits the ExecuteNonQuery
method, but its after that. I am not sure why this is happening. If anyone have anything to share please...
Here's the stack trace:
at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed)
at System.Data.SqlClient.SqlParameter.GetMetaTypeOnly()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
According to the reference provided UInt32
is not supported in Sql.
Inferring a
SqlDbType
fromUInt32
is not supported.
So it is good to pass parameter as;
command.Parameters.Add("@param1", SqlDbType.Int).Value = Convert.ToInt32(paramValue);
As you can try this:
cmd.Parameters.Add(new SqlParameter("@param", SqlDbType.Int));
You can check this Link for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With