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