Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Session state can only be used when enableSessionState is set to true

Reply
Thread Tools

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

 
 
KevinGravelle@gmail.com
Guest
Posts: n/a
 
      05-12-2006
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.

 
Reply With Quote
 
 
 
 
Alvin Bruney
Guest
Posts: n/a
 
      05-12-2006
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
-------------------------------------------------------

<> wrote in message
news: 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.
>



 
Reply With Quote
 
KevinGravelle@gmail.com
Guest
Posts: n/a
 
      05-12-2006
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)

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      05-12-2006
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/
===================================
<> wrote in message
news: 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)
>



 
Reply With Quote
 
KevinGravelle@gmail.com
Guest
Posts: n/a
 
      05-16-2006
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.

 
Reply With Quote
 
KevinGravelle@gmail.com
Guest
Posts: n/a
 
      05-17-2006
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.

 
Reply With Quote
 
conveda@gmail.com
Guest
Posts: n/a
 
      05-25-2006

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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[newbie] Session state can only be used when enableSessionState is set to true jon80 Software 0 03-08-2009 04:40 PM
"Session state can only be used when enableSessionState is set to true" Error Paul ASP .Net 3 02-11-2007 10:20 PM
Session state can only be used when enableSessionState is set to true Error Rick ASP .Net 0 03-28-2005 10:47 PM
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive David Lozzi ASP .Net 3 08-10-2004 12:44 AM
Session state can only be used when EnableSessionState is set to true... error John Baughman ASP .Net 6 12-24-2003 05:50 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57