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

Posted on 2007-08-01
Medium Priority
Last Modified: 2010-08-05
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=;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.
Question by:MKPanchal
LVL 15

Expert Comment

ID: 19607566
This related discussion thread has some workarounds mentioned, which I hope would help you:


Author Comment

ID: 19607725
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"))

Expert Comment

ID: 19609390
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

LVL 25

Accepted Solution

DBAduck - Ben Miller earned 1500 total points
ID: 19609548
I would put the enableSessionState="true" in your web.config.



Author Comment

ID: 19660378
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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question