[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dealing with a null result from a SQL qry

Posted on 2005-05-13
5
Medium Priority
?
184 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:indy500fan
  • 3
5 Comments
 
LVL 9

Expert Comment

by:william007
ID: 14001091
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
0
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 2000 total points
ID: 14001099
Technically this is not good flow control but (the line the error is on would be helpful):

If dr.HasRows Then
                While dr.Read()
                    Try
                            Attempt = dr.Item("attempt")
                           'Debug.WriteLine(dr.Item("LastName"))
                            txtAttemptNumber.Text = Attempt
                    Catch ex As Exception
                            txtAttemptNumber.Text = 1
                   
                    End Try
                End While
End If


An alternative is to say something like ...

If  dr.Item("attemp") Is DBNull.Value
0
 
LVL 9

Expert Comment

by:william007
ID: 14001103
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
0
 

Author Comment

by:indy500fan
ID: 14001129
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!
0
 
LVL 9

Expert Comment

by:william007
ID: 14001141
Nevermind, we learn together:)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 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