473,785 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for null value gives unexpected error

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.InvalidC astException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.Visua lBasic.Compiler Services.Operat ors.CompareObje ctEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form 1.Button1_Click (Object sender, EventArgs e) in C:
\...\Form1.vb:l ine 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
2 2120
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.Invalid CastException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.Visu alBasic.Compile rServices.Opera tors.CompareObj ectEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form 1.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
On Sep 28, 11:06 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
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.InvalidC astException: Operator '=' is not defined for type
'DBNull' and string "".
at
Microsoft.Visua lBasic.Compiler Services.Operat ors.CompareObje ctEqual(Object
Left, Object Right, Boolean TextCompare)
at my_project.Form 1.Button1_Click (Object sender, EventArgs e) in C:
\...\Form1.vb:l ine 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3259
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start erroring out. I see two different errors. One is a "Object reference not set to an instance of an object." error, which appears to always contain the same information, and the other is a "There is no row at position X.", where X is a number. Is this...
9
1846
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
21
21225
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence an integer) into an array. To prevent rechecking already processed cells, the script iterates through the (sorted) array to see whether that integer is an element of the array. After reading about javascript arrays a bit more, I thought...
7
2153
by: Filips Benoit | last post by:
Hi, TBL_CONTACT_PERSON CNTP_ID (auto) CNTP_LAST_NAME (required = yes) CNTP_FUNCTION (required = no) CNTP_..... (all required = no) FRM_CONTACT_PERSON_ADD_NEW Property DATA ENTRY = YES
7
7057
by: Douglas Buchanan | last post by:
I cannot access certain column values of a list box using code. I have a list box 'lstPrv' populated by the query below. SELECT tblPrv.fkPrvID, lkpCat.CatNm, lkpSrv.SrvNm, lkpCat.pkCatID, lkpSrv.pkSrvID FROM (lkpCat INNER JOIN lkpSrv ON lkpCat.pkCatID = lkpSrv.fkCatID) INNER JOIN tblPrv ON lkpSrv.pkSrvID = tblPrv.fkSrvD These are the columns displayed tblPrv.fkPrvID, lkpCat.CatNm, lkpSrv.SrvNm, lkpCat.pkCatID,
19
6810
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like what happens in memory operations, everyone knows the importance of freeing the allocated memory, but there do have memory leaks from time to
8
5461
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and do not compile with "Allow Unsafe Code Blocks=false" set to true, then the project compiles and no problems. BUT if I compile with "Allow Unsafe Code Blocks=true" then I het the error below. With unsafe {} of unsave{} removed it dos not solve the...
5
18968
by: John Smith | last post by:
Hey folks, I know this is an old topic, but I can't find a definitive answer on google. How do I tell if an int has been initialized or not? I had been testing it like: if(myInt == Convert.ToInt32(null)){ : }
1
214
by: c0mrade | last post by:
Try something like this... list = for n, it in enumerate(list): if not it: print 'Error on this definition' else: print '%d. %s' % (n+1, it) Results: 1. lkdfjsldk Error on this definition
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.