I am calling a stored procedure written at sql server, in my c# service. But I am again and again facing exception:
InvalidCastException was unhandled by user code: Specified cast is not valid
Code:
public function(Data dt)
{
con = new SqlConnection(constring);
string brand = dt.brand;
cmd = new SqlCommand("execute pro100 @brand, @check", con);
SqlParameter param = new SqlParameter("@check", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cmd.Parameters.Add("@brand", brand);
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
int result = (int)cmd.Parameters["@check"].Value; // Exception is here
con.Close();
return result;
}
My stored procedure is as follows This is the stored proc
ALTER PROCEDURE [dbo].[pro100]
@brand varchar(20),
@check int output
as
update carlog set minex=1000 where brand=@brand;
select @check=id from carlog where brand=@brand;
return @check
Could someone suggest the possible solution maybe?
Here it is a solution that ignores exception handling: