Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

Dealing with a null result from a SQL qry

Friends,

I have the following code, but when I run it, and the query that its data is derived returns a row, and that row has a value of Null

If dr.HasRows Then
                While dr.Read()
                    Attempt = dr.Item("attempt")
                    If Attempt = Nothing Then
                        txtAttemptNumber.Text = 1
                    Else
                        'Debug.WriteLine(dr.Item("LastName"))
                        txtAttemptNumber.Text = Attempt
                    End If
                End While
End If

I get the following error:

Cast from type 'DBNull' to type 'Integer' is not valid.

Can anybody see how to fix this, please?

Regards,
Eric
Avatar of william007
william007

try this..
  While dr.Read()
                   
                    If dr.Item("attempt")= Nothing Then
                        txtAttemptNumber.Text = 1
                    Else
                        'Debug.WriteLine(dr.Item("LastName"))
                        Attempt = dr.Item("attempt")
                        txtAttemptNumber.Text = Attempt
                    End If
                End While
ASKER CERTIFIED SOLUTION
Avatar of NetworkArchitek
NetworkArchitek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also try this.
If dr.HasRows Then
                While dr.Read()
                    Attempt = dr.Item("attempt")
                    If Attempt is dbnull.value Then
                        txtAttemptNumber.Text = 1
                    Else
                        'Debug.WriteLine(dr.Item("LastName"))
                        txtAttemptNumber.Text = Attempt
                    End If
                End While
End If
Avatar of indy500fan

ASKER

william007, your solutions didn't work unfortunately, but that was mainly due to the fact that I led you down the wrong path, and for that I am sorry.  I am so new at this, I forgot to mention as a work around I was trying to declare a variable Attempt as integer, reading the record from dr and if the result was something do something.  And for all that and wasting your valuable time and resources, I'm sorry.

 NetworkArchitek, you saw through what I was doing and the last line: If  dr.Item("attempt") Is DBNull.Value worked perfectly.

Thank you to you both.  I learn so much from everyone here!
Nevermind, we learn together:)