Archive for the ‘Session’ Category

Session state can only be used when enableSessionState is set to true

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.

This error usually occurs when you have declared an object variable public to the page and it access a Session variable, example:

Partial Class _Default

Inherits System.Web.UI.Page

Private m As New DBAccess(Session(”dbname”))

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

End Sub

End Class

just rewrite the above code to look like as below
Partial Class _Default
Inherits System.Web.UI.Page
Private m As DBAccess
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
m =New DBAccess(Session(”dbname”))
End Sub
End Class


, ,

No Comments


How to check if the ASP.Net Session has expired

  1. protected void OnInit(EventArgs e)
  2.   {
  3.        base.OnInit(e);
  4.    //It appears from testing that the Request and Response both share the 
  5.    // same cookie collectionIf I set a cookie myself in the Reponse, it is 
  6.    // also immediately visible to the Request collection.  This just means that
  7.    // since the ASP.Net_SessionID is set in the Session HTTPModule (which 
  8.    // has already run), thatwe can't use our own code to see if the cookie was 
  9.    // actually sent by the agent with the request using the collection. Check if 
  10.    // the given page supports session or not (this tested as reliable indicator
  11.    // if EnableSessionState is true), should not care about a page that does 
  12.    // not need session
  13.    if (Context.Session !=null)
  14.    {
  15.    //Tested and the IsNewSession is more advanced then simply checking if 
  16.    // a cookie is present, it does take into account a session timeout, because
  17.    // I tested a timeout and it did show as a new session
  18.    if (Session.IsNewSession)
  19.     {
  20.        // If it says it is a new session, but an existing cookie exists, then it must 
  21.        // have timed out (can't use the cookie collection because even on first
  22.        // request it already contains the cookie (request and response
  23.        // seem to share the collection)
  24.      string</span> szCookieHeader <span>=</span> Request.Headers["Cookie"];
  25.     if((null!= szCookieHeader)  (szCookieHeader.IndexOf("ASP.NET_SessionId")<= 0))
  26.      {
  27.       Response.Redirect("sessionTimeout.htm");
  28.      }
  29.     }
  30.    }
  31.   }
Source

, ,

No Comments



SetPageWidth