Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sql

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()
like image 431
Manish Rawat Avatar asked Jan 06 '16 08:01

Manish Rawat


2 Answers

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);
like image 113
Irshad Avatar answered Oct 03 '22 04:10

Irshad


As you can try this:

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

You can check this Link for more.

like image 23
Suvro Avatar answered Oct 03 '22 04:10

Suvro