• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2253
  • Last Modified:

Converting IsNull from VB6: operator is not valid for type 'dbnull' and type 'integer'

Hello Experts,

VB.NET newbie here...

How does this code translate correctly from VB6? -
If (Not IsNull((rs2("QTY LEFT").Value))) then

The conversion process turned the code into -
If (Not IsDBNull(rs2.Fields("QTY LEFT").Value)) then

... and that's when I get the error
0
sph3rion
Asked:
sph3rion
  • 6
  • 4
  • 2
  • +2
1 Solution
 
wraith821Commented:
If Not(IsDBNull(rs2.Fields("[QTY LEFT]").Value)) then
0
 
amyhxuCommented:
If (Not IsDBNull(rs2.Fields("[QTY LEFT]"))) then
0
 
b1xml2Commented:
If Not rs2.Fields("QTY LEFT").Value Is DBNull.Value Then

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wguerramCommented:
Have you just tried the original code:

If (Not IsNull((rs2("QTY LEFT").Value))) then

Since you are using recordsets, maybe work...
0
 
sph3rionAuthor Commented:
None of those are working, this is very confusing

immediate window says that the value of that field is '{system.dbnull}'
0
 
b1xml2Commented:
well then,
If Not rs2.Fields("QTY_LEFT").Value Is DBNull Then

End If

or

If Not rs2.Fields("QTY_LEFT").Value.GetType() = GetType(DBNull) Then

End If
0
 
sph3rionAuthor Commented:
not working either

I tried to assign the value in the field to an intTest (integer) and the compiler said I can't cast an integer as dbnull

I don't get it, .NET clearly sees that the value in the field is dbnull, yet I cannot capture that?
0
 
b1xml2Commented:
hmmmm
Best Shot
Dim value As Integer
If Not (rs2.Fields("QTY_LEFT") Is DbNull) Then
  value = DirectCast(rs2.Fields("QTY_LEFT"),Integer)
End If
0
 
sph3rionAuthor Commented:
I'll try that, in the meantime I'm increasing the point value

The root of the issue is that I have a table with QTY LEFT(type float) and at the very beginning of the process all values in that field in the table are NULL. I need to check for that NULL before I can enter into some code within the IF statement.

This seems like a fairly simple thing to do, it certainly is in VB6 so why is it so hard in .NET?
0
 
b1xml2Commented:
no it's simple. If you use a DataTable,
then the syntax would be:

If Not row.IsNull("QTY_LEFT") Then

End If
0
 
sph3rionAuthor Commented:
Anyone got any solutions for dealing with null values in recordsets in VB.NET?

It's not super important that I upgrade this app to .NET but I wanted to try anyway.

This type of thing happens all over this app which is just something that calculates dates automatically based on other data
0
 
wguerramCommented:
This is the way to use it.

'DBNull.Value

If Not rs2.Fields("QTY_LEFT").Value Is DBNull.Value Then

End If
0
 
sph3rionAuthor Commented:
That was already mentioned in this question and does not work.

0
 
sph3rionAuthor Commented:
I'm accepting b1xml2's answer because after some research this is how it should be done in .NET - with data tables instead of a connected recordset. Quite a change in philosophy from VB6 and I'll be educating myself on .NET when I can. For now, VB6 serves our needs best because we need to use recordsets.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now