System.Web.HttpException: Session state can only be used when enableSessionState is set to true

starstarstarstarstarstarstarstarstarstar Rating: 0/5 (0 vote cast) print
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive]
System.Web.UI.Page.get_Session() +147
WebForm.WebForm1.RegisterUser() +13
WebForm.WebForm1..Page_Load(Object sender, EventArgs e) +39
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +29
System.Web.UI.Page.ProcessRequestMain() +724
profile
KINGSLEY TAGBO
 : KINGSLEY TAGBO     : O'FALLON, Missouri, United, States   Reply  

Replies (17)

profile
KINGSLEY TAGBO

Hi :

Are you running this solution in a multi threade environment?

Also, can you do a simple Response.Write("Text") and confirm that you can actually output text to your web page.

Thanks


Yes the solution is running in Multi Threaded environment and when we write Response.write("message").. we are getting the message.

Please suggest what to do in this regard.

: stamada    Reply

profile
KINGSLEY TAGBO

Are you able to execute the code Response.Write() and write content to the page that is executing the thread?


WSS takes tight control of the configuration of your webserver. If you have it installed on the same server as other .Net applications, simply setting enableSessionState to true is insufficient.

Here is a related problem. http://www.kdkeys.net/system-web-httpexception-session-state-can-only-be-used-when-enablesessionstate-is-set-to-true/#link-6844

Following the steps in that kb article should clear up any issues with using the session object with a server that has WSS installed.


One thing you should understand is that Session variables are only available after the HttpApplication.AcquireRequestState event has happened. Here are a few lines of code to get Session variable info from a base page class.

'Base Page Class

Public Class BasePage

Inherits System.Web.UI.Page

Private strValue as String

Protected ReadOnly Property MyValue() As String

Get

Return strValue

End Get

End Property

Protected Sub Page_Loaded()

strValue = System.Web.HttpContext.Current.Session("MyVariable")

End Sub

End Class

'Code Behind

Public Class Page1

Inherits BasePage

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Page_Loaded()

Label1.Text = MyValue

End Sub

End Class

This is just a simple example. Hope it helps.

: JoeS    Reply

but I can't do a "response.write"...

Any Clue ?

Thanks

bs

: bs    Reply

Instead of System.Web.HttpContext.Current.Session("MyVariable")

use

System.Web.HttpContext.Current.Response.Write("Some text")

: smh    Reply

Hello,

I having the same error in my application, but it is not a multi-tread enmvironment..

I would like to use Session inside a library,

Someone can help me?

Regards

Gustavo M Rodrigues

: gmachado    Reply

profile
KINGSLEY TAGBO

Add a reference to System.Web.dll

Use System.Web.HttpContext.Current.Session to reference the current session


Hello Tagbo,

I read all the post above and did the same thinks, So When I use System.Web.HttpContext.Current.Session to deal with a session the error message change

from :"...Session state can only be used when enableSessionState is set to true, ..." to this message:"Object reference not set to an instance of an object."

The interesting thinks is that the error message occurs only in a few pages... It´s stranger to me...!!

Can you help me?

Regards

Gustavo M Rodrigues

: gmachado    Reply

I´m thinking that I found the problem, in some pages I´m creating the object with New operator, and in anothers pages I´m not using the new operator..

In that pages with new I recebe the error message, and in anothers no...

What do you think about this?

Regards

Gustavo

: gmachado    Reply

I have a solution. let me know if you haven't figured it out yet.

: knapster    Reply

I haven't figured it out yet

: dotlamer    Reply

well, now that I read each post, I see that people are having totally different issues. i was referring to the situation where you have a base class for all your pages, such as:

public class PageBase : System.Web.UI.Page

{

public PageBase()

{

//Session info is not available in the constructor...only in OnInit

Response.Write("asdfasdf") <--- fails

}

protected override void OnInit(EventArgs e)

{

Response.Write("asdfasdf") <--- works

}

}

hope that is self explanatory and answers your questions

: knapster    Reply

i m looking for B.E final year project preferring vb.net,c#,php & ASP.Net.plz suggest me some topics.


profile
KINGSLEY TAGBO

Cause :

You are trying to use a session or use a session's properties in a class and ASP.NET cannot get a reference to a session object

Resolution :

Use System.Web.HttpContext.Current.Session to refer to the session instance for the current http request instead of the Session Keyword . System.Web.HttpContext.Current.Session provides you with the System.Web.HttpSessionState instance for the current HTTP request.

This instance may not be available if you add a class and to your .NET project , consequently you may have to request for an instance of the session avilable for the context of the current http request.


As per the resoultion given for the Article we have used System.Web.HttpContext.Current.Session instead of session keyword. when we try to run the application we are getting the following error

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 45: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Line 46: 'Put user code to initialize the page here

Line 47: sDomainName = System.Web.HttpContext.Current.Session(Global.sConMainMenu)

Line 48:

Line 49: If sDomainName = "Carrier" Then sDomainName = "AgencyCarrier"

Please let me know what to do in the regard.

Thank you in advance.

Regards,

Sreedhar Tamada

: stamada    Reply


Post A Reply

 Questions & Answers