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

I want to use the Session("XX") variable in all pages for the connection of SQL server. In login page i have defined the variable and assign the value, but when i try to use the same i got the error message

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

I have defined the 'enablesessionstate="true"' in the page directvie. My code is like below

Dim MCKP As New Data.SqlClient.SqlConnection(Session("Intranet"))

My login.aspx page declare the value like below
Session.Add("Intranet", "Data Source=192.168.1.1;Initial Catalog=Trumac;Persist Security Info=True;User ID=sa;Password=xyz)
Please guide me how to use the same, So i dont need to define on every page user name, password etc.
MKPanchalAsked:
Who is Participating?
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
I would put the enableSessionState="true" in your web.config.

http://authors.aspalliance.com/das/web.config.aspx

Ben.
0
 
deepaknetCommented:
This related discussion thread has some workarounds mentioned, which I hope would help you:

http://www.velocityreviews.com/forums/t292042-session-state-can-only-be-used-when-enablesessionstate-is-set-to-true.html
0
 
MKPanchalAuthor Commented:
Hi Deepknet,
I have gone through the same document but it does not help me. I have already declared the same variable in login.aspx. I can get the session("Intranet") value in the page_load event. But i cant use the same at
Dim MCKP As New Data.SqlClient.SqlConnection(Session("Intranet"))
0
 
apb2Commented:
Hi MKPanchal
Have you thought about using the global.asax to set this variable on the Session Start (this could be the default value). Rather than setting the enablesessionstate setting in the page directive on all your pages, you could do this in your web.config page and if the page doesn't require session state then set this value to false in the page directive.

<pages enableSessionState="true"></page>

Hope that helps

apb1
0
 
MKPanchalAuthor Commented:
I can use applicaion or session only in procedure or function etc. but in the Inherits System.Web.UI.Page i cant use the same. So i need to defind at all procedure, function etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.