Session State in ASP.NET

Session State in ASP.NET using C#

In this tutorial, you will learn about session state in ASP.NET.

Definition:

Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead.

Syntax:

Session [“anytime”] = “misnames”;

Benefits of Session State

Session state is maintained at the session level.

The session state value is available on all pages within a user session.

Session’s information is stored on the server.

Session state persists the data of a particular user in the server.

This data available till user close the browser or session time completes.

Understanding Session State

To understand the concept of sessions, let’s take an example using code through. So for this follow the steps:

Step 1: Open Visual Studio.

Step 2: Select New Project from the left panel.

Step 3: Select Visual C# and then from .Net Frameworks select ASP.NET Web Application set the name and location and press OK.

Step 4:Select Empty and check web Form and press OK.

Step 5:From solution explorer selects your project name and right-click on it, then select add and then add the Web From.

Add a new web form in Visual Studio

Step 6:Put a name on the web page.

The name for web forms, visual studio

Step 7: pick two text boxes and one button from the toolbox. Textbox to get a value and then we store it in the session variable and after submitting new page is open where we check that value which we put on the last page this tell us how the session works on all over the site.

Put some values in text boxes so that they are same in session variables and we can get them, made on the next page.

Now run the project from the demo.aspx page.

Click on submit button.

ASP.net session output

This value appears on page2 its just because of session state. Server store session value and when we call it again on another page it will retrieve from the server. Session state is useful for password protected sites very security is an important issue.