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

Posted on 2007-08-01
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

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

    Author Comment

    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"))
    LVL 6

    Expert Comment

    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 24

    Accepted Solution

    I would put the enableSessionState="true" in your web.config.


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now