Link to home
Start Free TrialLog in
Avatar of csl002
csl002Flag for United States of America

asked on

NullReferenceException when checking for empty dataset

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

Avatar of Ashley Bryant
Ashley Bryant
Flag of United States of America image

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

Avatar of csl002

ASKER

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

Avatar of csl002

ASKER

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'
After making that change, is this line the one throwing the error?

If GlobalInfoReturn1.Tables(0).Rows.Count = 0 Then
ASKER CERTIFIED SOLUTION
Avatar of Ashley Bryant
Ashley Bryant
Flag of United States of America image

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
Avatar of csl002

ASKER

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

Thanks again,

Casey