csl002
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
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
ASKER
In the last code snippet it gave me a nullreference when it got to this line...
If GlobalInfoReturn1.Tables.C ount > 0 Then
PS. GlobalInfoReturn1 at that point is 'nothing'
If GlobalInfoReturn1.Tables.C
PS. GlobalInfoReturn1 at that point is 'nothing'
After making that change, is this line the one throwing the error?
If GlobalInfoReturn1.Tables(0 ).Rows.Cou nt = 0 Then
If GlobalInfoReturn1.Tables(0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Thanks so much for your help. Any way you could explain a bit why that was happening?
Thanks again,
Casey
Thanks again,
Casey
Open in new window