6

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?

7
  • because there are no "YourAssessment" or "MyAssessment" key in Session collection Commented May 10, 2012 at 14:28
  • ok my session getting null, but what action i shoud do if it's getting null? Commented May 10, 2012 at 14:54
  • well the best thing you can do is put value in session ;) just joking, where do you set session variables, show your code Commented May 10, 2012 at 14:57
  • let me explain my code above, wait.. Commented May 10, 2012 at 15:04
  • @AntonioBakula: u see my code? it's posted on your demand... Commented May 10, 2012 at 16:58

7 Answers 7

11

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Even then browsers can clear sessions/cookies. Life is hard working with browsers.
@CodingBarfield, that why I mentioned as long as the client doesn't clear it
yes i understand it, but in my case i cleared it session.abandon on log out only..
ok my session getting null, but what action i shoud do if it's getting null?
You should go back to the main page or whereever you initiated your session variables.
3

Always check for null when accessing Session object!
You can write some small utility that can be used for that:

public class SessionData
{
    public static T Get<T>(string key)
    {
        object value = HttpContext.Current.Session[key];

        if(value == null)
            return default(T);

        try
        {
            return (T)value;
        }
        catch(Exception e)
        {
            return default(T);
        }
    }

    public static void Put(string key, object value)
    {
        HttpContext.Current.Session[key] = value;
    }
}

Session can be null if app pool is recycled. That can happen because numerous reasons...

One trick for keeping your server from loosing session is making "pings" to server from javascript. It can make requests to some dummy url (empty page, or if you're perf freak, to .ashx handler) every minute or so. It can be useful for pages that you keep open for long time, like huge edit forms.
Also, beware, there are different session timeout values for debug and release configuration!

Comments

1

If Session is not null, please recheck that it has key "YourAssessment" and "MyAssessment".

Comments

1

First you can use you code like this

if (!bool.Parse(Session["YourAssessment"].ToString()) && 
     bool.Parse(Session["MyAssessment"].ToString()))
    Response.Redirect("~/myAssessment.aspx");

You sure you Sessions are not null

Check like this

if (Session["YourAssessment"] != null && Session["MyAssessment"] != null && 
    !bool.Parse(Session["YourAssessment"].ToString()) && 
     bool.Parse(Session["MyAssessment"].ToString()))
        Response.Redirect("~/myAssessment.aspx");

3 Comments

But how can session be null in 5 mint
if you use improc session, maybe worker process on IIS is restarted
ok my session getting null, but what action i shoud do if it's getting null?
1

When your Session expires the objects you have placed in your session such as Session["YourAssessment"] become null and the .toString() method call on those objects will then throw an Object reference error. To work around this you must first check to make sure your session variables being null before attempting to perform the toString().

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

2 Comments

ok my session getting null, but what action i shoud do if it's getting null?
Sorry, I never saw your question come through. If you are getting null for "Your Assessment" then you best bet is to redirect to a custom error page and display an error to the user that indicates that they have not chosen an assessment. Without knowing the full details of the app it's tough to say how it should be handled for sure but all applications should gracefully display errors to the user. If you don't wish to display a specific error to the user then redirect or display an error on the page itself indicating that "there was an error" and log it for yourself somewhere meaningful.
1

Instead of the .ToString and Boolean.Parse do Convert.ToBoolean(Session["YourAssessment"])

When I try Boolean b = Convert.ToBoolean(null) b = false ;)

Comments

1

Well after all that have been written about this it seems that problem is IIS application restarting and if your session is stored inproc that can couse deletion of session variables. So try to log application end events and see if this is a case, put this in Global.asax.cs application_end event, this code would log application restart and why it happened :

protected void Application_End(object sender, EventArgs e)
{
  HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);

  string shutDownMessage = "";

  if (runtime != null)
  {
    shutDownMessage = Environment.NewLine + "Shutdown: " +
                      (string)runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null) + 
                      Environment.NewLine + "Stack: " + Environment.NewLine +
                      (string)runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
  }

  string logFile =  HttpContext.Current.Server.MapPath(~/AppEndLog.log");
  string logMsg = "";
  if (File.Exists(logFile))
    logMsg = logMsg + File.ReadAllText(logFile) + Environment.NewLine + Environment.NewLine;
  logMsg = logMsg + Environment.NewLine + "ApplicationEnd - " + DateTime.Now.ToString() + shutDownMessage;
  File.WriteAllText(logFile, logMsg);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.