0

I am trying to use a stored procedure that returns an int. Then I need to convert int to a boolean (success).

SqlParameter Return = new SqlParameter(); 
Return.ParameterName = "@return";
Return.SqlDbType = SqlDbType.Int;
Return.Direction = ParameterDirection.ReturnValue;   

However this line of code throws a System.InvalidCastException error

 Success = (Boolean)Command.Parameters["@return"].Value;

What should I do to not get this error?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Roux
  • 1
  • 1

1 Answers1

0

You can use any of the following method to do what you need:

Success = Convert.ToBoolean(Command.Parameters["@return"].Value);

or

Success = Command.Parameters["@return"].Value==0?false:true;

or

if(Command.Parameters["@return"].Value==0)
  Success = false;
else
  Success = true;

(they're all the same thing as Convert.ToBoolean which will be false if the value is zero, otherwise true (this came from C!)

EDIT: one thing I have forgotten is that, if it returns a string or an object, you might have to cast it to integer first before converting to boolean.