How to use Session ?

Discussion in 'ASP .Net' started by fniles, May 31, 2007.

  1. fniles

    fniles Guest

    I am using Visual Studio 2005. In my ASPX page, when I try to use Session, I
    got the 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 <configuration>\<system.web>\<httpModules> section in the
    application configuration."
    In my Web.config I put the following, but still I got the error above:

    <system.web>
    <pages autoEventWireup="true" enableSessionState="true"
    enableViewState="true" enableViewStateMac="true" smartNavigation="true"
    validateRequest="false">
    <controls>
    <add tagPrefix="asp" namespace="System.Web.UI"
    assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    </controls>
    </pages>
    <compilation debug="true">
    :
    </compilation>
    <httpHandlers>
    :
    </httpHandlers>
    <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
    System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    </httpModules>
    <!-- Enable in-process session state for application -->
    <sessionState mode="InProc" cookieless="false" timeout="20"/>
    </system.web>

    How can I use the Session ? Thank you.
     
    fniles, May 31, 2007
    #1
    1. Advertising

  2. re:
    !> How can I use the Session ?

    Why don't you try a simple web.config, like :

    ------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.web>
    <pages enableSessionState = "true" />
    <compilation debug="true" />
    <sessionState mode="InProc" cookieless="false" timeout="20" />
    </system.web>
    </configuration>
    ----------------------------------------------------------

    Check to see if it works, and then add other configuration
    attributes until you find the one which is interfering ?

    To check whether it's working or not, in any page, in Page_Load, set a session variable :

    ---------------------------------------------
    <%@ Page Language="VB" %>
    <script language="VB" runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs)
    Session("somevar") = "somevalue"
    SessionState.Text = Session("somevar")
    End Sub
    </script>
    <html>
    <head>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Label id="SessionState" runat="server"/></asp:Label> <br />
    </div>
    </form>
    </body>
    </html>
    -----------------

    ....and retrieve the value in a Label in the same page.

    In this example, if Session is working, you'll see the text "somevalue" returned.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    "fniles" <> wrote in message news:...
    >I am using Visual Studio 2005. In my ASPX page, when I try to use Session, I got the 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 <configuration>\<system.web>\<httpModules> section
    > in the application configuration."
    > In my Web.config I put the following, but still I got the error above:
    >
    > <system.web>
    > <pages autoEventWireup="true" enableSessionState="true" enableViewState="true"
    > enableViewStateMac="true" smartNavigation="true" validateRequest="false">
    > <controls>
    > <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions,
    > Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    > </controls>
    > </pages>
    > <compilation debug="true">
    > :
    > </compilation>
    > <httpHandlers>
    > :
    > </httpHandlers>
    > <httpModules>
    > <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
    > Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    > <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    > </httpModules>
    > <!-- Enable in-process session state for application -->
    > <sessionState mode="InProc" cookieless="false" timeout="20"/>
    > </system.web>
    >
    > How can I use the Session ? Thank you.
    >
     
    Juan T. Llibre, May 31, 2007
    #2
    1. Advertising

  3. fniles

    fniles Guest

    Thank you.
    I needed the other stuffs in the web.config because I am using AJAX.
    I fixed the problem by making the <pages> simpler like the following:

    <system.web>
    <pages enableSessionState="true">
    <controls>
    <add tagPrefix="asp" namespace="System.Web.UI"
    assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    </controls>
    </pages>
    <compilation debug="true">
    :
    </compilation>
    <httpHandlers>
    :
    </httpHandlers>
    <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
    System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    </httpModules>
    <!-- Enable in-process session state for application -->
    <sessionState mode="InProc" cookieless="false" timeout="20"/>
    </system.web>

    Thank you.

    "Juan T. Llibre" <> wrote in message
    news:%...
    > re:
    > !> How can I use the Session ?
    >
    > Why don't you try a simple web.config, like :
    >
    > ------------------------------------------------------------
    > <?xml version="1.0" encoding="utf-8" ?>
    > <configuration>
    > <system.web>
    > <pages enableSessionState = "true" />
    > <compilation debug="true" />
    > <sessionState mode="InProc" cookieless="false" timeout="20" />
    > </system.web>
    > </configuration>
    > ----------------------------------------------------------
    >
    > Check to see if it works, and then add other configuration
    > attributes until you find the one which is interfering ?
    >
    > To check whether it's working or not, in any page, in Page_Load, set a
    > session variable :
    >
    > ---------------------------------------------
    > <%@ Page Language="VB" %>
    > <script language="VB" runat="server">
    > Sub Page_Load(Sender As Object, E As EventArgs)
    > Session("somevar") = "somevalue"
    > SessionState.Text = Session("somevar")
    > End Sub
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:Label id="SessionState" runat="server"/></asp:Label> <br />
    > </div>
    > </form>
    > </body>
    > </html>
    > -----------------
    >
    > ...and retrieve the value in a Label in the same page.
    >
    > In this example, if Session is working, you'll see the text "somevalue"
    > returned.
    >
    >
    >
    >
    > Juan T. Llibre, asp.net MVP
    > asp.net faq : http://asp.net.do/faq/
    > foros de asp.net, en español : http://asp.net.do/foros/
    > ======================================
    > "fniles" <> wrote in message
    > news:...
    >>I am using Visual Studio 2005. In my ASPX page, when I try to use Session,
    >>I got the 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 <configuration>\<system.web>\<httpModules> section in
    >> the application configuration."
    >> In my Web.config I put the following, but still I got the error above:
    >>
    >> <system.web>
    >> <pages autoEventWireup="true" enableSessionState="true"
    >> enableViewState="true" enableViewStateMac="true" smartNavigation="true"
    >> validateRequest="false">
    >> <controls>
    >> <add tagPrefix="asp" namespace="System.Web.UI"
    >> assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    >> PublicKeyToken=31bf3856ad364e35"/>
    >> </controls>
    >> </pages>
    >> <compilation debug="true">
    >> :
    >> </compilation>
    >> <httpHandlers>
    >> :
    >> </httpHandlers>
    >> <httpModules>
    >> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
    >> System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
    >> PublicKeyToken=31bf3856ad364e35"/>
    >> <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    >> </httpModules>
    >> <!-- Enable in-process session state for application -->
    >> <sessionState mode="InProc" cookieless="false" timeout="20"/>
    >> </system.web>
    >>
    >> How can I use the Session ? Thank you.
    >>

    >
    >
     
    fniles, Jun 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smythe
    Replies:
    3
    Views:
    1,255
    Jeff Smythe
    Jan 2, 2004
  2. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,291
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  3. sunkrajesh
    Replies:
    0
    Views:
    356
    sunkrajesh
    May 31, 2009
  4. Jazzis
    Replies:
    2
    Views:
    250
    Jazzis
    Sep 23, 2003
  5. Ted Byers
    Replies:
    0
    Views:
    583
    Ted Byers
    Dec 21, 2011
Loading...

Share This Page