Title: How do I correctly check DBNull in VB?
Tags: asp.net;.net;vb.net;sql-server-2008
Question: Why does the following code:
A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
results in the following error:
Conversion from type 'DBNull' to type 'String' is not valid.
When AndAlso is supposed to short-circuit according to this article:
http://support.microsoft.com/kb/817250
Comment: What type is CurRow by the way? anIDataReader/SqlDataReader?

Comment: Can you try this `A = (not IsDBNull(CurRow("BuyBook"))) AndAlso CType(CurRow("BuyBook"), string) = "Yes"`

Comment: Go through [this](http://stackoverflow.com/questions/221582/most-efficient-way-to-check-for-dbnull-and-then-assign-to-a-variable) post.

Comment: I've even broken it into two statements and tried. I think IsDBNull is broken.

Comment: debug the code to see the data inside the cells.

Comment: Is there any other code related to this operation. if yes then post it.

Comment: Try NOT (CurRow("BuyBook")) is System.Dbnull.Value, it should work

Here is the accepted answer: You are correct. AndAlso is short circuiting.
However, the error comes by calling CurRow("GuyBook") (verify this in a debugger to make sure I'm not a liar or making some crazy assumptions or just misremembering* ;-). Before you ask for a value, you need to ask the DataRow if it has a value. That is, use:
CurRow.IsNull("BuyBook")
Happy coding.
*One should just be able to compare with DBNull.Value or use IsDBNull. However, I am fairly certain that I ran into a row before that threw these exceptions instead of returning a DBNull object. Start by finding out -- in the Immediate Window of the Debugger -- exactly which expression throws the exception.

Here is another answer: If dt.Rows(0)("BuyBook") = DBNull.Value Then
End If
Hope it helps.

Here is another answer: In vb.net I usually do something like this:
A = (CurRow("BuyBook") &amp; "" = "Yes")
Old trick from vb6 era. If CurRow("BuyBook") is null, vb.net will consider it as empty string when concatenating it with another string.
Comment for this answer: Doesn't this rely upon Option Strict being off?  I would prefer just using the ToString method directly `CurRow("BuyBook").ToString = "Yes"` if using this solution.

Here is another answer: Have you tried comparing like this:
If CurRow("BuyBook") Is DBNull.Value Then
'...
End If
Comment for this answer: Updated to changed the comparison done to match how its done in some of our code.
Comment for this answer: Yes, I get an exception saying that you can't compare an Object and DBNull type.

Here is another answer: Instead of not IsDBNull(CurRow("BuyBook")), use NOT (CurRow("BuyBook")) is System.Dbnull.Value). Try this:
A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
Comment for this answer: I guess you missed this `I've even broken it into two statements and tried`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 5, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:01/13/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net><.net><vb.net><sql-server-2008>', 'Title': 'How do I correctly check DBNull in VB?', 'ViewCount': 35756, 'id': 8847114, 'user_AboutMe': '<p>I am a student. A perpetual student.</p>\n\n<p>For the convenience of those who Google and copy &amp; paste resulting URL, feel free to try my plugins for <a href="https://addons.mozilla.org/en-US/firefox/addon/remove-google-redirect-in-goog/?src=ss" rel="nofollow noreferrer">Firefox</a> and <a href="https://chrome.google.com/webstore/detail/remove-google-redirect-in/miaghkkhkjklnijffegcpjlhdjelnkke" rel="nofollow noreferrer">Chrome</a>.</p>\n\n<p>I have recently released my primary project <a href="https://github.com/PlatformLab/Arachne" rel="nofollow noreferrer">Arachne</a>, a lightning-fast cooperative threading library. Please give it a whirl and <a href="https://github.com/PlatformLab/Arachne/issues" rel="nofollow noreferrer">create an issue</a> if you see any problems.</p>\n\n<p>I have also written a few simple tools, such as one for <a href="https://github.com/hq6/smux" rel="nofollow noreferrer">tmux automation</a>, an <a href="https://github.com/hq6/column" rel="nofollow noreferrer">improved version</a> of the venerable Unix column, and a tool for <a href="https://github.com/hq6/Colorize" rel="nofollow noreferrer">adding color to text</a> in the terminal based on user-specified patterns.</p>\n', 'user_AccountId': 166796.0, 'user_DisplayName': 'merlin2011', 'user_Reputation': 68612.0, 'user_Views': 3504.0, 'user_id': 391161.0}}