1
votes

I'm trying to get a Session["user"] on Page_Load but it keep giving me this crash:

'Session' threw an exception of type 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 \\ section in the application configuration.

Here is my web.config

<configuration>
<system.web>
<pages enableSessionState="true" />        
    <httpModules>
      <add name="Session" type="System.Web.SessionState.SessionStateModule" />
    </httpModules>
</system.web>
</configuration>

There are other things inside the configuration tag, but the important part is this one, where the config is correct, but the error still the same.

Why is this happening?

No big deal in the .aspx

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (Session["user"] == null)
            Response.Redirect("~/Login.aspx");
    }
}
6
Is it possible the page in question exists in a sub-folder which has it's own web.config file (ie. a sub-application)?CodingGorilla
Could it be an issue in IIS' settings on the server?JB King
Edited with the .aspx page - No, it's not a sub-folder or something like thatKyore

6 Answers

3
votes

I just had a similar problem and found a solution at That Hyphenated Website of all places. My code:

    void Application_Error(object sender, EventArgs e )
    {
        Exception ex = Server.GetLastError();
        if ( ex != null )
        {
            if ( ex.GetBaseException() != null )
            {
                ex = ex.GetBaseException();
            }

            if ( Session != null )  // Exception occurred here.
            {
                Session[ "LastException" ] = ex;
            }

            Toolbox.Log( LogLevel.Error, "An Application Error Occurred", ex );
        }
    }

... and by changing the indicated line to:

            if ( HttpContext.Current.Session != null )

the code now performs as I would expect. Caveat Emptor: my link to that hyphenated website behaves differently on this page than it does from the page of Google results to my query

'session' threw an exception of type 'system.web.httpexception'
0
votes

did you have Seesionstate tag in your web.config?try adding this tag.

Sample:

        <sessionState mode="Off|InProc|StateServer|SQLServer"
          cookieless="true|false"
          timeout="number of minutes"
          stateConnectionString="tcpip=server:port"
          sqlConnectionString="sql connection string"
          stateNetworkTimeout="number of seconds"/>

For More details:http://msdn.microsoft.com/en-us/library/ms178586.aspx

0
votes

If your using win2008 and IIS the session state might not be enabled by default. Can you check that the Session State Mode Settings is enabled in IIS?

Here is a picture of what your looking for:

enter image description here

Right click and select Open Feature and make sure it is set to enabled.

0
votes

Try setting EnableSessionState="true" in page directive

<%@ Page EnableSessionState="true" %>
0
votes

Start and Stop of "ASP.NET State Service" helped me out in a similar situation.

0
votes

Convert.ToString() V/S obj.ToString()

Use Convert.ToString(), because if you are using obj.ToString() and the object(obj) value is null it will throw an excetion of type "System.NullReferenceException". The cause of the exception is null doesn't have a method called ToString().

Response.Write("Name : " + Session["User_Name"].ToString()); //Here it will throw an Exception if Session["User_Name"] is null.

Response.Write("Name : " + Convert.ToString(Session["User_Name"])); //Here it will not throw any Exception.

Refer this link: http://burnignorance.com/asp-net-developer-tips/some-best-practices-while-writing-asp-net-code/