EagleFox
EagleFox

Reputation: 1367

Specified cast is not Valid - bit value has NULL from stored procedure

I use data from a stored procedure that returns two columns: Paid and New

They both contain bit values. So, it returns 0 or 1, but some test data return a NULL.

Paid  New
 1     1
 0     0
 1    NULL

As a result I am getting this error

Specified cast is not valid

How would I resolve this issue in c# or better yet in the stored procedure itself, so that the stored procedure would only return 0 or 1.

Upvotes: 1

Views: 1897

Answers (1)

Sasha
Sasha

Reputation: 8850

SELECT Paid, ISNULL(New, 0)

This will return 0 instead of NULL in column "New". That is fix in stored procedure.

As for C# fix, you can cast nullable bit to nullable bool (bool?):

(bool?)sqlValue

Upvotes: 3

Related Questions