Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does session object throw a null reference exception?

on some of my aspx page I am checking session like this

if (bool.Parse(Session["YourAssessment"].ToString()) == false
    && bool.Parse(Session["MyAssessment"].ToString()) == true)
{
    Response.Redirect("~/myAssessment.aspx");
}

It works fine if I keep playing with the pages frequently, but if I don't do anything with the page at least even for 5 min, running the page throws the error

Object reference not set to an instance of an object.

Following is the stack for this

[NullReferenceException: Object reference not set to an instance of an object.]
   yourAssessment.Page_Load(Object sender, EventArgs e) in d:\Projects\NexLev\yourAssessment.aspx.cs:27
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Could some body explain me this weird behaviour?

And as we know by default the session last for is 20 min.

EDITED

See I have a page default aspx, it has got a button which fixes on the some basis where to redirect On default page it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        Session["YourAssessment"] = false;
        Session["MyAssessment"] = false;
    }
}

on button click it has

protected void imgClientFreeEval_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
{
    if (HttpContext.Current.Request.IsAuthenticated)
    {
        string sqlQuery = "SELECT count(*) FROM SurveyClient WHERE UserID='"
            + cWebUtil.GetCurrentUserID().ToString() + "'";
        SqlParameter[] arrParams = new SqlParameter[0];
        int countSurvey = int.Parse(
            Data.GetSQLScalerVarQueryResults(sqlQuery).ToString());
        if (countSurvey > 0)
        {
            Session["YourAssessment"] = true;
            Session["MyAssessment"] = false;
        }
        Response.Redirect((countSurvey > 0)
            ? "~/yourAssessment.aspx"
            : "~/myAssessment.aspx");
    }
    else
    {
        Response.Redirect("~/login.aspx");
    }

and on myAssessment page it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && bool.Parse(Session["YourAssessment"].ToString())
            && !bool.Parse(Session["myAssessment"].ToString()))
        {
            Response.Redirect("~/yourAssessment.aspx");
        }
    }
}

and on yourAssessmtn it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && !bool.Parse(Session["YourAssessment"].ToString())
            && bool.Parse(Session["MyAssessment"].ToString()))
        {
            Response.Redirect("~/myAssessment.aspx");
        }

        PopulateAllSurveyByUser();
        if (ViewState["surveyClientID"] != null)
        {
            grdSurveyDetail.Visible = true;
            PopulateSurveyDetails(
                int.Parse(ViewState["surveyClientID"].ToString()));
        }
        else
        {
            grdSurveyDetail.Visible = false;
        }
    }
}

what's wrong please explain?

like image 705
NoviceToDotNet Avatar asked May 10 '12 14:05

NoviceToDotNet


1 Answers

You first need to check whether that session variable exists

if(Session["YourAssessment"] != null)
    // Do something with it
else
    // trying to call Session["YourAssessment"].ToString() here will FAIL.

That happens since your session has a lifecycle, which means - it expires (the cookie that defines it expires) - thus your objects vanish. you could increase sessionState timeout in web.config for your sessions to last longer.

For example, in web.config

  <system.web>
      <sessionState timeout="40" />
  </system.web>

Will make your sessions last for 40 minutes, as long as the client doesn't clear it, and the web server is up&running.

like image 51
Shai Avatar answered Sep 21 '22 04:09

Shai