Accessing session variables in different pages

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?

    Requires Free Membership to View

    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

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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.

This was first published in January 2005