Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,

I stuck on this error message. can any one give me a hint?

in my code behind(ASP.net C# web form programming), I try to evaluate a session variable, then execute some codes... debug pops this error message(see below) every time when it runs into the session evaluation line. What is proper setting in webconfig to fix this? can u give a complete sample. I tried as message suggested, it still pops.

if (Session["name"] == NULL)
//Do sth

error message
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

Thank you in advance

dd
Posted

1 solution

Follow Session state can only be used when enableSessionState is set to true either in a configuration[^], which says...
Quote:
Did you enable the session state in the section as well?

XML
<system.web>
      <pages enablesessionstate="true"> 
 </pages></system.web>

Or did you add this to the page?

XML
<%@Page enableSessionState="true"> 

And did you verify that the ASP.NET Session State Manager Service service is running? In your screenshot it isn't. It's set to start-up mode Manual which requires you to start it every time you want to make use of it. To start it, highlight the service and click the green play button on the toolbar. To start it automatically, edit the properties and adjust the Start-up type.

Or set the SessionState's mode property to InProc, so that the state service is not required.

XML
<system.web>
      <sessionState mode="InProc">
 </system.web>

Check MSDN for all the options available to you with regards to storing data in the ASP.NET session state.

Note: It's better to have your Controller fetch the value from the session and have it add the value to the Model for your page/control (or to the ViewBag), that way the View doesn't depend on the HttpSession and you can choose a different source for this item later with minimal code changes. Or even better, not use the session state at all. It can kill you performance when you're using a lot of async javascript calls.
 
Share this answer
 
v2
Comments
dd88gratefull 6-May-13 11:24am    
tadit.thank u so much.i am not using Mvc model. so i don't have controller. I did set pages enablesessionstate =”TRUE” and sessionstate mode=“InPROc”.did not work for me. any samp code?thankuso much

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