Asp.net session variable

Amaranth picture Amaranth · Mar 8, 2012 · Viewed 69.9k times · Source

I have a asp.net project with c# code behind. I have a static class called GlobalVariable where I store some information, like the currently selected product for example.

However, I saw that when there are two users using the website, if one changes the selected product, if changes it for everybody. The static variables seem to be commun to everybody.

I would want to create (from the c# code) some kind of session variable used only from the c# code, but not only from the page, but from any class.

Answer

driis picture driis · Mar 8, 2012

Yes static variables are shared by the whole application, they are in no way private to the user/session.

To access the Session object from a non-page class, you should use HttpContext.Current.Session.