Solved

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

Posted on 2007-04-02
8
2,879 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
Independent Software Vendors: 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 250 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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