Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

System.NullReferenceException when checking Count of DataTable rows

Posted on 2007-10-01
3
Medium Priority
?
346 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:dba123
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
TSmooth earned 2000 total points
ID: 19993384
That could be a number of things. It could be that "GetInfo" or "GetPhoneLIst" aren't returning anything and thus dtPhones and/or dtPhonestest are set to nothing. It could also be that Request.QueryString("LeadID") is not returning anything. Keep in mind that query string parameters are case sensitive.

To find out exactly what is the culprit, use the debugger and step through these lines one by one to see if dtPhones and dtPhonestest are being assigned actual values.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 20019681
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.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 20023984
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question