Using Session in Asp.Net

Asp.Net Session Management

As soon as a user requests a page on our site, a session is started on the server for that user. For this, a Session object is created on the server and the information carried in this object throughout the user's session can be accessed from each page.

This object and the information it contains are destroyed as soon as the user terminates the session.

A different Session object is generated for each user, so the information stored with this object is user specific. A SessionId value is generated for each Session object on the server, so it can be understood from which user new requests come. SessionId information is stored on both client and server sides and matching is done accordingly in case of new incoming requests.

We can add information to Session object with code.

Adding Data into Session Object

As can be seen in the examples below, we can add information to the session with the Add method of the Session class. The parameter name and value of the information to be added are specified in the method brackets.

Session.Add("name", TextBox1.Text);
 
Session.Add("time", DateTime.Now.ToString());
 
Session.Add("city", "Istanbul");

Reading Data In The Session Object

We can access information in the Session object as follows.

Label1.Text = Session["name"].ToString();

In case there is no information with that name in the session, we can use a code similar to the following.

 if ( Session["name"] != null )
          Label1.Text = Session["name"].ToString();

 

asp.net session management, using session object in asp.net, login, logoff, session management, session object, what is session

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 665 times.

Online Users: 349



asp-net-using-session