Manish Rawat
Manish Rawat

Reputation: 1212

The parameter data type of UInt32 is invalid. (MS SQL Server)

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

Upvotes: 5

Views: 8089

Answers (4)

Mahedee
Mahedee

Reputation: 166

UInt16 is not supported in ADO.NET. Use Int64 instead of UInt16. For more information visit: https://msdn.microsoft.com/library/yy6y35y8(v=vs.100).aspx

Upvotes: 1

GunslingerMSV
GunslingerMSV

Reputation: 11

You can pass it like string. Use UInt32.ToString(). It's work perfectly.

Upvotes: 1

As you can try this:

cmd.Parameters.Add(new SqlParameter("@param", SqlDbType.Int));

You can check this Link for more.

Upvotes: 1

Irshad
Irshad

Reputation: 3131

According to the reference provided UInt32 is not supported in Sql.

Inferring a SqlDbType from UInt32 is not supported.

So it is good to pass parameter as;

command.Parameters.Add("@param1", SqlDbType.Int).Value = Convert.ToInt32(paramValue);

Upvotes: 11

Related Questions