Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a webpage in ASP.NET C# that creates a session variable using
Session["State"]="State1";


I try to access this session variable in another page called WaitPage and it works.

When i try to access this Session variable in a thread in WaitPage or try to access the session variable in a timed event, I get 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.

I have already enabled "EnableSessionState=true" in both webpages.
I have the required add name="Session" type="System.Web.SessionState.SessionStateModule" in httpModules section in web.config.

Why am I getting this error? Can you help me fix this?

Thanks
Jobin Thomas
Posted
Updated 26-Feb-11 8:01am
v5

You cannot directly access the Session variable in a Thread.

Session is associated with HttpContext.Current object. When a Http request is under process a HttpContext.Current is instantiated and thus the HttpContext.Current.Session is instantiated. These objects gets out of scope when a http request is proceesed and page is rendered.

In Threads that are started on a page event runs async and there the HttpContext.Current.Session is null. so you cant access the Session object.

One way :
A ashx or aspx page would be created to do the session manipulation and let the Thread calls that aspx/ashx page through a WebRequest.
 
Share this answer
 
Comments
[no name] 26-Feb-11 14:12pm    
Do you have a solution file, code example you can show me? I dont really know how to do it.
_Ashish 27-Feb-11 4:39am    
pl see your next post regarding this problem. i've tried to make a solution
http://www.codeproject.com/Answers/162558/How-to-keep-rechecking-session-variable-in-a-page-.aspx#answer2
Espen Harlinn 27-Feb-11 5:38am    
Right - my 5
Error in itself is quite self-explanatory.

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive...
This simply means,
You need to include a <pages... > attribute in web.config:
XML
<pages enableSessionState = "true" />

OR
Just Enable SessionState in the <%@ Page ...%> directive in your ASPX page, like:
XML
<%@ Page enableSessionState = "true" %>
 
Share this answer
 
Comments
[no name] 26-Feb-11 11:38am    
I have already enabled "EnableSessionState=true" in both webpages.
I have the required add name="Session" type="System.Web.SessionState.SessionStateModule" in httpModules section in web.config.

Updated the question.
Sandeep Mewara 26-Feb-11 11:40am    
Do a IISreset and try. :)

For now, leaving those options, without looking at the environment configured, it would be difficult to tell the reason now. :doh:
You may need to enable session store.

Refer this

http://support.microsoft.com/kb/317604[^]
 
Share this answer
 
Comments
[no name] 26-Feb-11 11:40am    
I think the link you posted refers to SQL usage. I do not use SQL in my code at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900