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
LVL 1
sph3rionAsked:
Who is Participating?
 
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
 
wraith821Commented:
If Not(IsDBNull(rs2.Fields("[QTY LEFT]").Value)) then
0
 
amyhxuCommented:
If (Not IsDBNull(rs2.Fields("[QTY LEFT]"))) then
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
b1xml2Commented:
If Not rs2.Fields("QTY LEFT").Value Is DBNull.Value Then

0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.