0

When I try to use a Session veriable in InitializeCulture, I get the folowing error:

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.

This is the code where the error is thrown

    public class BasePage : Page
{
    public string Lang { get; set; }

    protected override void InitializeCulture()
    {
        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["lang"]))
            {

                Session["lang"] = Request.QueryString["lang"].ToLower();
            }


            if (Session["lang"] != null && !string.IsNullOrEmpty((string)Session["lang"]))
            {
                Lang = (string)Session["lang"];
                string selectedCulture = Lang == "nl" ? "nl-BE" : "fr-BE";

                UICulture = selectedCulture;
                Culture = selectedCulture;

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture);
            }

            base.InitializeCulture();
        }
    }
}

I changed my Web.Config, as sugested in the error. But It keeps throwing the same error.

<pages  enableSessionState="true">
                <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
        </pages>
        <httpModules>
            <remove name="Session" />
            <add name="Session" type="System.Web.SessionStateModule" />
            <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter"/>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>

Does anyone has a solution? Thanks!!!

Vinzcent
  • 1,438
  • 6
  • 33
  • 59

1 Answers1

1

Try to set enableSessionState=true in the pages section.

<system.web>
    <pages enableSessionState="true"> 
</system.web>

Also, if you're using IIS7 on Windows 2008, your modules should be inside the <system.webServer> section, instead of <system.web>.

rla4
  • 1,228
  • 13
  • 25
  • Have you checked if the ASP.NET State Service is running? – rla4 Oct 22 '13 at 16:16
  • 1
    Also, if you're using IIS7 on Windows 2008, your modules should be inside the section, instead of - http://stackoverflow.com/questions/2757403/error-using-session-in-iis-7 – rla4 Oct 22 '13 at 16:23