Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Session object in ASP.NET

I've just been given a new task to bootstrap a website created by someone else. But I'm absolutely new to Web. The website is in ASP.NET,C#. The code itself is not hard to understand except for the Session object. I don't understand where, how and why it's used.Could please someone explain the usage of Session object with a possible example?

P.S. What would these two lines mean?

    lblPensValue.Text = sh.pensDec((string)Session["connSTR"], 113, 23);
and
    if ((string)Session["connSTR"] == null)
like image 762
Mikayil Abdullayev Avatar asked Jun 08 '11 05:06

Mikayil Abdullayev


People also ask

What is the use of session object?

You can use the Session object to store information needed for a particular user session. Variables stored in the Session object are not discarded when the user jumps between pages in the application; instead, these variables persist for the entire user session.

What is the use of session in C#?

Session is a feature in ASP.NET Core that enables us to save/store the user data. Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.

Is it necessary to create session object?

For external applications, you must create a Session object, if you do not have an Entity object. If you want to use the AdminSession object, the same rule applies.


2 Answers

Session is used to store data for the user's session on the web site. (this data store is per-user-browser session, and is subject to being wiped at any time by various application events)

It is generally used to store information across multiple page views in a user's session (ie. visit) to your website.

It can be used anywhere in code that runs in the context of the user's session; meaning inside a page, or in the appropriate application lifecycle events which run in the context of a session (such as Session Start)

As for your samples;

The first one, I can't fully explain, as I do not know what the function sh.pensDec() is supposed to do.

The second one is checking to make sure there is a value stored in that session variable, before running the code that follows.

like image 146
Andrew Barber Avatar answered Oct 03 '22 19:10

Andrew Barber


HTTP by nature is stateless. The WebServer doesn't know any details after it processes the request and sends back to the client. Thus, any subsequent requests are like fresh requests to the server.

To Enable the Server to remember & subsequently recognize what it served to the client, ASP.NET uses various mechanisms of which Session is one of them.

Session is created per user. So, in your Page, you are fetching the "connSTR" are storing it. Whenever a subsequent request comes from the same user, by querying Session with the key

Session["connSTR"]

you get back its value. Since Session is an Object, its casted as a string in your code.

like image 37
Akhil Avatar answered Oct 03 '22 19:10

Akhil