NullReferenceException when checking for empty dataset

csl002
csl002 used Ask the Experts™
on
I use a function that returns a dataset, if I don't have enough information from the user I return 'nothing' from the fuction. So back in my code I am checking if the returned dataset=nothing. That is when I get a NullReferenceException.

'this is the code for checking for an empty dataset

If GlobalInfoReturn1.Tables(0).Rows.Count = 0 Then
                MessageLabel.Text = "Make sure you have filled out all fields correctly"
            Else
                Do something else

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ashley BryantSenior Software Engineer

Commented:
First, make sure that you even have a table.

If GlobalInfoReturn1.Tables.Count > 0 Then
     If GlobalInfoReturn1.Tables(0).Rows.Count = 0 Then
          MessageLabel.Text = "Make sure you have filled out all fields correctly"
     Else
          'Do something else
     End If
Else
     MessageLabel.Text = "Make sure you have filled out all fields correctly"
End If

Open in new window

Author

Commented:
Ok, but that didn't help
 

'Getting back database dataset from query
            Dim GlobalInfoReturn1 As DataSet = GetGsDs(pn1)
            If GlobalInfoReturn1.Tables.Count > 0 Then
                If GlobalInfoReturn1.Tables(0).Rows.Count = 0 Then
                    MessageLabel.Text = "Make sure you have filled out all fields correctly"
                Else
                    'Extracting dataset to table
                    Dim DescTable1 As DataTable = GlobalInfoReturn1.Tables(0)

                    'Assigning description to text box
                    desc1.Text = DescTable1.Rows(0)("DESCRIPTION").ToString
                    SortCode1.Text = DescTable1.Rows(0)("CODE_SORT").ToString
                    BINLOC1.Text = DescTable1.Rows(0)("BIN").ToString
                    Dim costInt As Double = CalcCosts(DescTable1.Rows(0)("AMT_COST"), DescTable1.Rows(0)("AMT_ALT_COST"), qty1.Text)
                    cost1.Text = costInt.ToString
                    qty2.Focus()
                End If
            Else
                MessageLabel.Text = "Make sure you have filled out all fields correctly"
            End If

Open in new window

Author

Commented:
In the last code snippet it gave me a nullreference when it got to this line...

If GlobalInfoReturn1.Tables.Count > 0 Then
 
PS. GlobalInfoReturn1 at that point is 'nothing'
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ashley BryantSenior Software Engineer

Commented:
After making that change, is this line the one throwing the error?

If GlobalInfoReturn1.Tables(0).Rows.Count = 0 Then
Senior Software Engineer
Commented:
OK.  Made my last post before seeing your reply.



If GlobalInfoReturn1.Tables.Count > 0 Then

//change to this

If Not GlobalInfoReturn1 Is Nothing AndAlso GlobalInfoReturn1.Tables.Count > 0 Then

Open in new window

Author

Commented:
That worked. Thanks so much for your help. Any way you could explain a bit why that was happening?

Thanks again,

Casey

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial