Session State in ASP.Net



Session State Management in ASP.Net

In this asp.net tutorials we will learn about Object of ASP.Net or Session State Management in ASP.Net.

State management is a important part of any application. The Session is the important features of asp.net.

Session state is a period of time to visit a website for a particular user. Session can store the client data on a server. Session is a best state management features to store the client data on server separately for each user.

  • Session can store value across on multiple pages of website.
  • Viewstate can store value on single page.

In simple word we can say, At the same time more than one users login to system, all user identification name store separately until they logout. session can store username or any unique identification of user for a login period time.

Session value can be accessible from all pages from website. We can store some information in session in one page and can access same information on rest of all page by using session.

  • By default, Session state is enabled for all ASP.NET applications.

Let’s understand session using example.

When you login to any website with username and password. your username shows on all other pages.The username will be stored in session and on page we access session value to display username.

Syntax of session

Session[“session_name”] = “session value”;


Declare session in asp.net

Session[“name”]=”Meera Academy”;
Response.Redirect(“nextpage.aspx”);

Above we create session with name parameter and assign value “Meera Academy” to session[“name”] and redirect user to other page “nextpage.aspx” using response.redirect method.

Retrieve session value on other page

string myvalue= Session[“name”].ToString();
Response.Write(“Name = ” + myvalue);

Here, we retrieve session[“name”] value and store session value in string variable “myvalue”.


Session Example on ASP.Net C#

Here, we take example to understand session in asp.net c#. Open visual studio and design two web page, one for assign value to session and other for retrieve value from session.

Session example in asp.net
Session example in asp.net

C# code for set session value on first page “default.aspx” on button click events. After assigning a value to session we redirect to other page “next.aspx” using response.redirect method. Here, we assign textbox name value to Session[“name”] and city value to Session[“city”] and redirect to next.aspx page.

protected void btnsession_Click(object sender, EventArgs e)
{
Session["name"] = txtname.Text;
Session["city"] = txtcity.Text;

Response.Redirect("next.aspx");
}

On “next.aspx” page we retrieve and display session value on web page using label control. C# code on “next.aspx” page for retrieve session value.

protected void btngetvalue_Click(object sender, EventArgs e)
{
lblname.Text = Session["name"].ToString();
lblcity.Text = Session["city"].ToString();
}

In above example we retrieve session value on next.aspx. you can retrieve session value all pages of website until you clear session or abandon session.

Related ASP.Net Topics :

Session State Example in C#.Net
View State in C#.Net


Subscribe us

If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.

We hope that this asp.net tutorial helped you to understand about Session State in C#.


Next asp.net tutorial we will learn about Session Timeout.


3 thoughts on “Session State in ASP.Net

Leave a Reply

Your email address will not be published. Required fields are marked *