title: c# how to set up and use session state from preinit
body: <p>OK so to set and read variables from the current session</p>
  
  <pre><code>String Myvar =(string) System.Web.HttpContext.Current.Session[“MyVariable”]
  </code></pre>
  
  <p>To set</p>
  
  <pre><code>System.Web.HttpContext.Current.Session[“MyVariable”] = “NewValue”
  </code></pre>
  
  <p>I can do neither, I get a <code>System.NullReferenceException: Object reference not set to an instance of an object.</code> from System.Web.HttpContext.Current.Session.  </p>
  
  <p>In my web.config I have </p>
  
  <pre><code>&lt;sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20"&gt;
  &lt;/sessionState&gt;  
  </code></pre>
  
  <p>I have read a dozen articles on the the necessity of <code>IHttpHandler</code> and an <code>IRequiresSessionState</code> interface.  I think the issue may be caused because I am requesting this information in <code>Page_PreInit</code>. I found a <strong><a href="https://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule">solution</a></strong> in a stack overflow article but I don't seem be using it properly to actually make this go.</p>
  
  <p>I am not sure what I am missing.  Thanks in advance.</p>