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"]));
jcook32Asked:
Who is Participating?
 
devsolnsConnect With a Mentor Commented:
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 LearnedConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.