C# Object reference not set to an instance of an object.

Posted on 2011-05-11
Last Modified: 2012-05-11
I have a page that seems to be coded properly, however every now and than I get the error below.
That is wrong with line 66 I don't get it.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 64:         BAL_Core cor = new BAL_Core();
Line 65:         DataSet ds = cor.Get_WO(field, uid, DAL_Core.filter);
Line 66:         int dscount = ds.Tables.Count;
Line 67:         if (dscount > 0)
Line 68:         {

Source File: d:\wwwroot\MOC2\wo.aspx.cs    Line: 66

Question by:quest_capital
    LVL 4

    Expert Comment

    by:Sathish DV
    can u add code for Get_WO(field, uid, DAL_Core.filter). Need to see any thing is missing from initialization?
    LVL 16

    Expert Comment

    by:Philip Pinnell
    If there is a problem with line 65, ie cor.Get_WO doesn't actually return a dataset line 66 will give the error you report.

    You should look at why a ds might not be returned.

    You could test ds to see if it is null/nothing before trying to use it
    LVL 9

    Expert Comment

    Check if you are passing valid values to BAL function Get_WO and if its returning proper dataset by filling it with data. Your dataset seems to be null.
    LVL 3

    Expert Comment

    Try this:
    if(ds != null && ds.Tables != null)
         int dscount = ds.Tables.Count;

    Open in new window

    Hope it helps.

    Accepted Solution

    I needed to use the Try,Catch & Finally method to open an close my db connections.

    Author Closing Comment

    I found the problem.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now