Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a bool using execute scalar

I have a stored procedure returning a boolean varialbe :

Here is my C# code that works when I try to return an int variable.But it doesnt work when I try it for bool.

DbCommand dbCommand = db.GetStoredProcCommand("spGetConfigureAlerts");
            object o = db.ExecuteScalar(dbCommand);
            bool item = o == null ? 0 : (bool)o;
            return item;

I have it as shown above , but on line three it says that :

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'bool'    

How can I solve it ?

like image 230
CodeNinja Avatar asked Jul 05 '26 08:07

CodeNinja


1 Answers

Why do you use 0 at all if you want to initialize a bool variable?

bool item = (o == null || DBNull.Value == o) ? false : (bool)o;
like image 104
Tim Schmelter Avatar answered Jul 07 '26 22:07

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!