Last post Aug 07, 2008 12:09 PM by RyanSmith345
Aug 04, 2008 08:51 PM|RyanSmith345|LINK
The DBNull has always caused me problems.
I thought this code would work fine:
If Not TypeOf (dr.Volume) Is System.DBNull Then
However when I enter that code, I get the build error:
Expression of type 'String' can never be of type 'System.DBNull'.
I've also tried:
If Not IsDBNull(dr.Volume) Then
But that just chokes at run time with the "dr.Volume is DBNull"
What's the right code here? This is driving me nuts!
Aug 04, 2008 10:15 PM|agolden|LINK
If dr.Volume = DBNull.Value Then
Hope that helps.
Aug 05, 2008 12:03 AM|RyanSmith345|LINK
Thanks for the suggestion, but at compile time I get the error:
Overload resolution failed because no accessible '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean': Value of type 'System.DBNull' cannot be converted to 'String'.
I've tried everything I possible can on this error with no luck.
I've finally had to resort to the ugle hack of:
s &= dr.Volume
Catch ex As Exception
I hate leaving this in my code, but so far it's the only solution that works at all. I would love to know the answer to this before I ship code that looks this hacky.
Thanks again for your help.
Aug 05, 2008 04:13 AM|Barry Andrew|LINK
IF NOT DbNull.Value.Equals(dr.Volume) THEN
Aug 05, 2008 05:20 AM|BlueJam|LINK
Does dr.Volume return a string, it might be worth just checking for String.Empty.
I think checking for DBNull.Value only works if you are checking against a return type of 'Object'.
Aug 05, 2008 08:18 AM|DkUltra|LINK
1) check if dr.volume is null in memory .. then check its value
Hope this helps
Aug 05, 2008 01:38 PM|RyanSmith345|LINK
Thanks for the suggests, but no luck on either.
The problem is it will throw an exception if you try to retrieve the value at all, hints why test "Is Nothing" will crash.
It's the same issue with:
I'm really at a loss here. I guess the Try..Catch blocks will have to stay for now and I can just go on looking like a sloppy coder.
Aug 05, 2008 01:46 PM|agolden|LINK
Ryan - What data type is dr?
Aug 05, 2008 05:09 PM|DkUltra|LINK
First check if the dr is null
If not dr is nothing then
If dr.Volume Is
Hope this helps
Aug 06, 2008 05:20 AM|BlueJam|LINK
Aug 06, 2008 11:33 AM|RyanSmith345|LINK
dr is a data row from a data table. Volume looks like:
<xs:element name="Volume" msprop:Generator_UserColumnName="Volume" msprop:Generator_ColumnPropNameInRow="Volume" msprop:Generator_ColumnVarNameInTable="columnVolume" msprop:Generator_ColumnPropNameInTable="VolumeColumn" minOccurs="0">
<xs:maxLength value="2147483647" />
When it throws the exception, dr.volume is indeed DBNull, but I don't understand why it's not letting me test for DBNull.
Aug 06, 2008 10:12 PM|agolden|LINK
If this is a datarow from a typed dataset, you should have a method for the row called IsVolumeNull() that you can check to see if it contains a null value.
Aug 07, 2008 12:09 PM|RyanSmith345|LINK
Don't I feel like an idiot. It's been a while since I've written that code and I forgot that there were built in functions of the typed dataset that did the test for me.
Odd that is so hard to Google the answer for. I feel much better about my code now.