Thursday, October 09, 2008

ASP.Net Enabling the Session Object

Today I was attempting to use the Session object to exchange information between two ASP .Net pages. I received the following exception when I tried to assign something to it:

System.Web.HttpException was unhandled by user code
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."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.Page.get_Session()
at AddToCart.SaveCustomFields() in c:\Inetpub\wwwroot\StarterSite\AddToCart.aspx.cs:line 485
at AddToCart.AddItemToCart() in c:\Inetpub\wwwroot\StarterSite\AddToCart.aspx.cs:line 490
at AddToCart.btnAddToCart_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\StarterSite\AddToCart.aspx.cs:line 300
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Based on information I found on the web, I first tried to set EnableSessionState="True" on every page that needed to use the session, and that didn't cure the issue.

Next, I tried setting <pages enableSessionState="true"> in the Web.config file under configuration/system.web. That didn't fix it either.

Finally, what worked was to put this into the Web.config:
<sessionState mode="InProc"></sessionState>
(also under configuration/system.web).

No comments: