473,287 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

What is wrong with this picture?

I'm trying to set my shopping cart text on my home page using the
following function that is executed when the class is constructed:

protected string fnGetShoppingCartText()
{
if (Session["Cart"] != null)
{
ArrayList cart = (ArrayList)Session["Cart"];

int intItemCount = cart.Count;

if (intItemCount == 0)
{
return "Shopping Cart (Empty)";
}
else if (intItemCount == 1)
{
return "Shopping Cart (1 item)";
}
else
{
return "Shopping Cart (" + intItemCount + " items)";
}
}
else
{
return "Shopping Cart (Empty)";
}
}

-----
It fails on the line: ArrayList cart = (ArrayList)Session["Cart"];

I have EnableSessionState="true" set in the Page Directive.

I am using MS Visual Web Developer 2005 Express Edition. I am able to
reference this variable in other pages.

Your help is greatly appreciated.

Thank you,
Kevin G.

May 12 '06 #1
6 15996
it should only fail on that line if the session object is of some other type
where the cast cannot work. Use a safe cast instead and then test for null.
ArrayList arr = Session["cart"] as ArrayList;
if(arr != null) etc

What exactly is the error message when the failure occurs?

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

<Ke***********@gmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
What is wrong with this picture?

I'm trying to set my shopping cart text on my home page using the
following function that is executed when the class is constructed:

protected string fnGetShoppingCartText()
{
if (Session["Cart"] != null)
{
ArrayList cart = (ArrayList)Session["Cart"];

int intItemCount = cart.Count;

if (intItemCount == 0)
{
return "Shopping Cart (Empty)";
}
else if (intItemCount == 1)
{
return "Shopping Cart (1 item)";
}
else
{
return "Shopping Cart (" + intItemCount + " items)";
}
}
else
{
return "Shopping Cart (Empty)";
}
}

-----
It fails on the line: ArrayList cart = (ArrayList)Session["Cart"];

I have EnableSessionState="true" set in the Page Directive.

I am using MS Visual Web Developer 2005 Express Edition. I am able to
reference this variable in other pages.

Your help is greatly appreciated.

Thank you,
Kevin G.

May 12 '06 #2
Thanks for your reply. Unfortunately, your suggestion did not work.
Here's the exact error message (using my original code):

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.

Source Error:
Line 89: {
Line 90:
Line 91: ArrayList cart = (ArrayList)Session["Cart"];
Line 92:
Line 93: if (cart != null)

May 12 '06 #3
You need to :

1. include a <pages... > attribute in web.config :

<pages enableSessionState = "true" />

*or*

2. enable SessionState in the <%@ Page ...%> directive in your page :

<%@ Page enableSessionState = "true" %>

Also, the System.Web.SessionStateModule should exist in your
<httpModules> section in the application configuration.

Unless you've erased it, it should be there.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<Ke***********@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Thanks for your reply. Unfortunately, your suggestion did not work.
Here's the exact error message (using my original code):

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.

Source Error:
Line 89: {
Line 90:
Line 91: ArrayList cart = (ArrayList)Session["Cart"];
Line 92:
Line 93: if (cart != null)

May 12 '06 #4
Still no go. I already had the enableSessionState directive at the top
of my aspx file, but I did not have the httpModules section in
web.config file. However, after adding this and rebuilding my project,
it still didn't work.

I am able to reference this same session variable on another aspx.cs
page (which also has the enableSessionState directive at the top of its
aspx file), so I'm not sure what the deal is. If you can think of
anything else for me to try, please let me know.

Thank you,
Kevin G.

May 16 '06 #5
I figured out the problem. I was trying to reference a session
variable that hadn't been initialized yet. I was trying to reference
the session variable in one of my page's class constructor before the
Session_Start function had been executed in the global.asax file.

Thanks for looking into this.

Kevin G.

May 17 '06 #6

Ke***********@gmail.com schrieb:
I figured out the problem. I was trying to reference a session
variable that hadn't been initialized yet. I was trying to reference
the session variable in one of my page's class constructor before the
Session_Start function had been executed in the global.asax file.

Thanks for looking into this.

Kevin G.


I just found out that the same error also occurs when using
Server.Transfer to redirect the handling of the request to another
page.

It seems to me that this is a severe error because there is no reason
why this should not work. Can someone please report that to Microsoft?
:)

May 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Didier Jeanson | last post by:
Hi, The session does not work in ASP.NET under Windows 2003 and IIS6. The page is very simple. - When I try to access the Session object in .NET I receive the following error: Session...
3
by: David Lozzi | last post by:
Error: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive at line 6 Line 4: <script RUNAT="SERVER"> Line 5: ...
1
by: Axel Dahmen | last post by:
Hi, I've added a property to my Page base class (this base class inherits from Page and all my aspx.cs classes inherit from my base class). This property of mine uses the Session object to...
0
by: Rick | last post by:
I am getting a "Session state can only be used when enableSessionState is set to true" error. it works on the first form where I am writing some data to a dictionary(Dictionarybase). After the...
2
by: ras26 | last post by:
I have a WebSite "App1" which has an external assembly "Lib1". Inside this external assembly we have developed some classes that derive from "System.Web.UI.Page". One of these pages is called...
3
by: Paul | last post by:
Hi All, In my application, I wished to check certain things on each page load, so rather than paste the same code in each pages constructor, I thought it would be more logical to inherit from...
2
by: fniles | last post by:
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...
10
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I've been on this for a while, and cannot figure it out. Can someone please help with this message? SessionState can only be used when EnableSessionState is set to true, either in a...
4
by: Cirene | last post by:
In my web.config I added <pages enableSessionState="true">. In each of my pages I also added EnableSessionState="True" to the Page declaration. (I didn't think this was necessary, but...) ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.