?
Solved

Operator '=' is not defined for type 'DBNull' and string "".

Posted on 2007-04-02
8
Medium Priority
?
2,884 Views
Last Modified: 2008-10-22
Hi Experts,

I am using the following code: -

If Not rs.Fields("Primary").Value = “” Then MSFlexGrid1.set_TextMatrix(MSFlexGrid1.Rows - 1, 4, rs.Fields("Primary").Value)

The data colum “Primary” sometimes contains no data. When I run the code it comes up: -

Operator '=' is not defined for type 'DBNull' and string "".

How do I resolve this please.

Many Thanks
Lee
0
Comment
Question by:ljhodgett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18838528
Try
If Not IsNull(rs.Fields("Primary").Value) Then...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838532
If rs.Fields("Primary").Value != DBNull.Value  Then MSFlexGrid1.set_TextMatrix(MSFlexGrid1.Rows - 1, 4, rs.Fields("Primary").Value)
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18838833
You may also want to check for the actual empty string....

If not (rs.Fields("Primary") Is System.DBNull.Value OrElse rs.Fields("Primary") = String.Empty) Then
  MSFlexGrid1.set_TextMatrix(MSFlexGrid1.Rows - 1, 4, rs.Fields("Primary").Value)
End if
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
r1937 earned 1000 total points
ID: 18838889
Try this instead:

If Not rs.Fields("Primary").Value Is DBNull.Value Then MSFlexGrid1.set_TextMatrix(MSFlexGrid1.Rows - 1, 4, rs.Fields("Primary").Value)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 18838979
If Not isDBNull(rs.Fields("Primary").Value) then
      If Not rs.Fields("Primary").Value = “” Then
            MSFlexGrid1.set_TextMatrix(MSFlexGrid1.Rows - 1, 4, rs.Fields("Primary").Value)
      end if
end if
0
 

Author Comment

by:ljhodgett
ID: 18839002
Hi,

Thank you for your comments. SirBounty's code does not seem to like "IsNull" I know this is a function of vb6 but it seems as if it has been removed from vb.net unless its called something else.

When I try AngelIII version it comes up: -

Error      97      Identifier expected.      C:\Documents and Settings\eljay\Desktop\vb.net\Stock Control Professional\A.1.1\frmViewSuppliers.vb      139      33      Stock_Control

And underlines the first = of the code.

Best Regards
Lee
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18839038
Import Microsoft.VisualBasic to get the old VB6 functions back...

The != operator is a C# thing... it should be "Not rs.Fields("Primary").Value = DBNull.Value  "

Or you could try mine :)
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18839356
I'm curious... what could the rest of us have done better to be awarded some of the points for this question?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question