3

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 ?

CodeNinja
  • 3,188
  • 19
  • 69
  • 112

3 Answers3

7

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;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
bool item = o == null ? false : (bool)o;
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
0

Try this:

bool item = o == null ? false : (bool)o;
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68