c# how to set up and use session state from preinit

Posted by Praesagus on Stack Overflow See other posts from Stack Overflow or by Praesagus
Published on 2009-08-27T18:32:56Z Indexed on 2013/11/08 9:55 UTC
Read the original article Hit count: 213

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET