An operand of type clash: int is incompatible with uniqueidentifier

I get a SQLException "An operand of type clash: int is incompatible with uniqueidentifier" when I try to execute the stored procedure below from C # code.

create proc sp_Get_Allfields_Account_Public @username varchar(20), @password varchar(20), @acc_num UniqueIdentifier out, @cust_name varchar(20) out, @balance float out as select @acc_num=acc_num,@cust_name=cust_name,@balance=balance from Account_Public where username=@username and password=@password 

C # code is as follows

 cmd = new SqlCommand("sp_Get_Allfields_Account_Public", con); cmd.CommandType = CommandType.StoredProcedure; // Add Input Parameters cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@password", password); // Add output parameters cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier); cmd.Parameters["@acc_num"].Direction = ParameterDirection.Output; cmd.Parameters.AddWithValue("@cust_name", SqlDbType.VarChar); cmd.Parameters["@cust_name"].Direction = ParameterDirection.Output; cmd.Parameters.AddWithValue("@balance", SqlDbType.Float); cmd.Parameters["@balance"].Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); 

Table definition

 create table Account_Public ( acc_num uniqueidentifier, cust_name varchar(20), username varchar(20), password varchar(20), balance float ) 
+4
source share
2 answers

This is because you call AddWithValue and then pass the value as an enum (which is interpreted as int ).

 cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier); 

Use a different method (maybe just Add ).

+9
source

This may seem a bit redundant, but are you sure that the data type of the [Account_Public].[acc_num] is actually a uniqueidentifier , not an int ?

Try this instead:

 cmd = new SqlCommand("select @acc_num=acc_num,@cust_name=cust_name,@balance=balance from Account_Public where username=@username and password=@password ", con); cmd.CommandType = CommandType.Text; 

with the same parameters. Are you getting the same error?

In addition, I highly recommend specifying explicit sizes in all char parameters. This is not so important for the input parameters, but it is very important for the weekend.

0
source

All Articles