0

OK so to set and read variables from the current session

String Myvar =(string) System.Web.HttpContext.Current.Session[“MyVariable”]

To set

System.Web.HttpContext.Current.Session[“MyVariable”] = “NewValue”

I can do neither, I get a System.NullReferenceException: Object reference not set to an instance of an object. from System.Web.HttpContext.Current.Session.

In my web.config I have

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20">
</sessionState>  

I have read a dozen articles on the the necessity of IHttpHandler and an IRequiresSessionState interface. I think the issue may be caused because I am requesting this information in Page_PreInit. I found a solution in a stack overflow article but I don't seem be using it properly to actually make this go.

I am not sure what I am missing. Thanks in advance.

Community
  • 1
  • 1
Praesagus
  • 2,029
  • 5
  • 30
  • 48
  • 3
    If you move that code to your Page_Load event does it work? Why do you need this in the PreInit? – Jim W Aug 27 '09 at 18:35
  • Make sure your ASP .Net State service is started. As even if you put code in Page_PreInit, it should work. Can you give more information? – Nirlep Aug 27 '09 at 20:38
  • 1
    One more thing which line you are using first. If you have not set session["MyVariable"] and you try to cast to (string). it will give you NUll reference error. So try putting NULL check condition before casting – Nirlep Aug 27 '09 at 20:41
  • I am swapping master pages - on PreInit is the only place I know how to do this. – Praesagus Aug 27 '09 at 21:12

3 Answers3

1

As the comment mentioned, is there some reason you need this in the PreInit event?

PreInit happens very early in the page lifecycle. It happens, in fact, before even the master page (if any) is applied, before all the controls are fully initialized, etc.

A much better choice for the majority of applications is in the Load event. If you're still getting a NullReferenceException there, then there's a bigger issue.

John Rudy
  • 37,282
  • 14
  • 64
  • 100
  • I think I do need to do it in PreInit. Can't I access state at that point? I understand that I can via the solution I linked to in the question. Unfortunately I am missing some small piece of that puzzle needed to implement it. – Praesagus Aug 27 '09 at 21:15
  • For swapping master pages, yes, you need to do it at PreInit. However, swapping master pages relies on session state for you? Is there some mechanism other than session state which you can use to get the information needed to swap the master page? – John Rudy Aug 27 '09 at 21:35
  • It's based on the user's admin level gathered at login. I suppose I could pass the information from page to page via post and hidden vars but...yuck. I'd be holding all my user info in state as well as passing it prehistorically. – Praesagus Aug 27 '09 at 21:54
1

You can access the session by implementing IRequiresSessionState interface in your class.

This is a flag interface, so you dont need to implement any extra code.

When you implement this asp.net will know that you want to access the session.

public partial class YOUR_ASPX: System.Web.UI.Page , IRequiresSessionState
{

... your code

}
Menol
  • 1,230
  • 21
  • 35
0

To access the session state pre-init you can do something like this. I use it so that I can have a different admin master than the regular user one. Each page has a method at the top.

PageTools tools = new PageTools();
protected void Page_PreInit(object sender, EventArgs e)
{
    tools.setMasterPage(Page, Context);
}

PageTools is my class that holds the method that chooses the appropriate mater page and has the http handler.

public void setMasterPage(Page page, HttpContext context)
    /***********************************************************************
     * Author   Daniel Tweddell
     * Date     9/18/09
     * 
     * Several of the pages are for non-admin use, however these pages will
     * also be used by the admin users and will need to have the admin menu
     * and such.  So based on the login, we either show the page with the
     * standard master or if the user is admin, use the admin master.
     ***********************************************************************/
    {
        if (context.Handler is IReadOnlySessionState || context.Handler is IRequiresSessionState)
        {
            context.Handler = Handler();
        }
        String sMasterPage="~/content/page.master";
        if (userinfo.IsUserAdmin) sMasterPage="~/content/administrator/admin.master";//make sure the user is admin
        page.MasterPageFile = sMasterPage; 
    }

Here is a step by step to setting up the httphandler. (which is the other thing you'll need.

Community
  • 1
  • 1
Praesagus
  • 2,029
  • 5
  • 30
  • 48