Accessing session variables in different pages
How do you access session variables in different pages of an application?
For example: I have Session("USerId") = "ABC" in page1.aspx. I go to Page2.aspx on clicking a button in page1. Can I use like this Dim sUser as string:
sUser = Session("UserId") in page2.aspx?
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial Director
You just use them and it just works. Are you having any problem with the code you sent? By the way, you have to enable session state in the web config in order for that to work (<sessionState mode="anything but OFF"). Recall that depending on your server configuration you may need to select different values. If you have a Web Farm, InProc will only work on a single machine, and you have to ensure following requests coming from the same user are redirected to the same server that answered the first time (LocalDirector from Cisco is a well-known product for that). Otherwise, you have to either select StateServer or SQLServer to persist the session information in a central location.
You can find a lot more about this and other fundamental concepts in ASP.NET in a book such as
this one.
Dig Deeper
-
People who read this also read...
This was first published in January 2005