Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operand type clash: int is incompatible with uniqueidentifier

I am getting an SQLException "Operand type clash: int is incompatible with uniqueidentifier" when I am trying to execute the below stored procedure 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
)
like image 534
Mohammad Nadeem Avatar asked Dec 28 '22 08:12

Mohammad Nadeem


1 Answers

It's because you're calling AddWithValue, then passing the value as the enum (which is interpreted as an int).

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

Use a different method (probably just Add).

like image 57
cjk Avatar answered Jan 27 '23 15:01

cjk