Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

System.NullReferenceException when checking Count of DataTable rows

Why do I get object reference error here if I've clearly defined my 2 data tables?

        Dim dtPhones As DataTable = GetInfo(Int32.Parse(syStudentID))
        Dim dtPhonestest As DataTable = GetPhoneList(CInt(Request.QueryString("LeadID")))


        If ((dtPhones.Rows.Count > 0) AndAlso (dtPhonestest.Rows.Count > 0)) Then

System.NullReferenceException: Object reference not set to an instance of an object.

it's referring to dtPhones.Rows.Count > 0
ASKER CERTIFIED SOLUTION
Avatar of TSmooth
TSmooth

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 Dustin Hopkins
From what you've given, it looks to me like the problem lays with the count method. You should try changing the "rows.count > 0" to "rows.count -1 > 0"
The reason is count, counts all the rows in the datagrid...including "0". So you must subtract 1 in order for the numbers to match.
So your code would look like this:
Dim dtPhones As DataTable = GetInfo(Int32.Parse(syStudentID))
        Dim dtPhonestest As DataTable = GetPhoneList(CInt(Request.QueryString("LeadID")))
        If ((dtPhones.Rows.Count - 1 > 0) AndAlso (dtPhonestest.Rows.Count - 1  > 0)) Then

If that doesn't fix it then you should what TSmooth said and debug values.
I'm sorry I misread what you were doing. what I wrote above was for if you were looping through the table. TSmooth is right, you should output the results of those functions to make sure they're outputing what you think they are.