By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Testing for null value gives unexpected error

P: n/a
Here is the code:

64 If objdr.Item("BD") = "" Or objdr.Item("BD") Is DBNull.Value Then
'test to see if result is blank or null
65 strBD = "00000000" 'No birthdate found so set to 8 spaces
66 Else
67 strBD = objdr.Item("BD") 'All ok so leave as is
68 End If

Here is the error I get:

System.InvalidCastException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.VisualBasic.CompilerServices.Operators.C ompareObjectEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form1.Button1_Click(Object sender, EventArgs e) in C:
\...\Form1.vb:line 64

I am sure I have used this before with no problems, so am a little
confused...

Any thoughts?

Thanks for your time.

Sep 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 28 Sep 2007 17:19:56 -0000, Problematic coder
<gn******@gmail.comwrote:
>Here is the code:

64 If objdr.Item("BD") = "" Or objdr.Item("BD") Is DBNull.Value Then
'test to see if result is blank or null
65 strBD = "00000000" 'No birthdate found so set to 8 spaces
66 Else
67 strBD = objdr.Item("BD") 'All ok so leave as is
68 End If

Here is the error I get:

System.InvalidCastException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.VisualBasic.CompilerServices.Operators. CompareObjectEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form1.Button1_Click(Object sender, EventArgs e) in C:
\...\Form1.vb:line 64

I am sure I have used this before with no problems, so am a little
confused...

Any thoughts?

Thanks for your time.
The exception tells you what is wrong. You are trying to compare a
DBNull to a string.

Try changing the If to:
If objdr.Item("BD") Is DBNull.Value OrElse objdr.Item("BD") = ""
Then

This tests for DBNull.Value first and changing Or to OrElse prevents
the execution of the second test if the first is True.
Sep 28 '07 #2

P: n/a
On Sep 28, 11:06 am, Jack Jackson <jacknos...@pebbleridge.comwrote:
On Fri, 28 Sep 2007 17:19:56 -0000, Problematic coder

<gnews...@gmail.comwrote:
Here is the code:
64 If objdr.Item("BD") = "" Or objdr.Item("BD") Is DBNull.Value Then
'test to see if result is blank or null
65 strBD = "00000000" 'No birthdate found so set to 8 spaces
66 Else
67 strBD = objdr.Item("BD") 'All ok so leave as is
68 End If
Here is the error I get:
System.InvalidCastException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.VisualBasic.CompilerServices.Operators.C ompareObjectEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form1.Button1_Click(Object sender, EventArgs e) in C:
\...\Form1.vb:line 64
I am sure I have used this before with no problems, so am a little
confused...
Any thoughts?
Thanks for your time.

The exception tells you what is wrong. You are trying to compare a
DBNull to a string.

Try changing the If to:
If objdr.Item("BD") Is DBNull.Value OrElse objdr.Item("BD") = ""
Then

This tests for DBNull.Value first and changing Or to OrElse prevents
the execution of the second test if the first is True.- Hide quoted text -

- Show quoted text -
Makes total sense, thank you for the clear explanation

Sep 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.