简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM