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=;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.
Who is Participating?
DBAduck - Ben MillerPrincipal ConsultantCommented:
I would put the enableSessionState="true" in your web.config.

This related discussion thread has some workarounds mentioned, which I hope would help you:
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"))
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

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.
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.