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(syStud entID))
Dim dtPhonestest As DataTable = GetPhoneList(CInt(Request. QueryStrin g("LeadID" )))
If ((dtPhones.Rows.Count > 0) AndAlso (dtPhonestest.Rows.Count > 0)) Then
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
it's referring to dtPhones.Rows.Count > 0
Dim dtPhones As DataTable = GetInfo(Int32.Parse(syStud
Dim dtPhonestest As DataTable = GetPhoneList(CInt(Request.
If ((dtPhones.Rows.Count > 0) AndAlso (dtPhonestest.Rows.Count > 0)) Then
System.NullReferenceExcept
it's referring to dtPhones.Rows.Count > 0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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(syStud
Dim dtPhonestest As DataTable = GetPhoneList(CInt(Request.
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.