Solved

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

Posted on 2007-04-02
8
2,864 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
8 Comments
 
LVL 67

Expert Comment

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

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now