[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

object reference not set to a reference of an object error.

I call this function from the page load function

            private void LoadPage()
            {
                  if (((int)(ViewState["ProjectID"])) >0)  error on this line
                  {
                        ProjectListDB.ProjectList Project = new ProjectListDB.ProjectList();
                        SqlDataReader myRS;
                        myRS = Project.GetSystems_ProjectList(((int)ViewState["ProjectID"]));
0
jcook32
Asked:
jcook32
2 Solutions
 
devsolnsCommented:
you need to check for null first,

if(ViewState["ProjectID"] != null)
{
               if (((int)(ViewState["ProjectID"])) >0)
               { ...}
}

your attempting to access an object where one has aparantly not been initialized.
0
 
Bob LearnedCommented:
1) Where are you storing it in the ViewState?

2) You can combine those if conditions:

if (ViewState["ProjectID"] != null && (int)(ViewState["ProjectID"]) > 0)
{
}

Bob


0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now