How to use Session Object in ASP.Net C#

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

The Session is the important Object of ASP.Net.

The Session used to store the session specific information for a Web site.

The Session Object is used to Store Variable Value on same Browser.

We generally used Session for Send Variable Value from one web form to other web form using Response.Redirect and Server.Transfer Method.

The Syntax of Session Object in ASP.Net

Declare Session Object in ASP.Net:

Session["Id"]=”Meera Academy”;
Response.Redirect(“NextPage.aspx”);
- Here the Id identify the Session and the “Meera Academy” is the value which we stored in session object.

 

Retrieve the Session Value on Other form:

string myvalue = Session["Id"].ToString();
Response.Write(“Your Name is  =  ” myvalue);

The Other Properties and Method of Session:

Session.Abandon()

Session.Abandon() used to destroys the session and we can not use that session till running web form.
The Session.Abandon() kills the current Session Id in ASP.Net.

 

Session.Clear()

Session.Clear() used to just removes all values (content) from the Session Object. The session with the same key is still alive.
We can use that same session Id after clear the Session. The session is alive Clear() Method just clear the Value of Session Object in ASP.Net.

 

Session.TimeOut() – asp.net session timeout

Session.TimeOut() used to set the Interval time in Minute if our Browser will stay idle position for a specific Time The as.net session timeout will be TimeOut automatically.

Session.Timeout = 2;    means if our page will stay up to 2 minute idle position the session value goes out.

If the user don’t do anything on web for for a long time then they automatically logged out from web site, this things done by the TimeOut property of Session in ASP.Net.

Here, we tack an example for more understanding about Session State.
Here, we will pass (send) TextBox control Value from one page to other page using Session Variable,
so, First Create a session and store the TextBox value to session and retrieve that session value to other page.

First Design the asp.net page like:

how to use session in ASP.Net with C#

how to use session in ASP.Net with C#

Now, write the below code on SEND Button click event on code behind page of asp.net:

protected void Button1_Click(object sender, EventArgs e)
{
Session["txt"] = TextBox1.Text;
Response.Redirect(“second.aspx”);
}

Here we first store the TextBox value to Session["txt"] and transfer user to other page.

how to use session in ASP.Net with C#

how to use session in ASP.Net with C#

The Below screen show the page layout where we can retrieve the session value.

how to use session in ASP.Net with C#

how to use session in ASP.Net with C#

Write below code in Get Value Button click event for retrieve the session value.

 protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Session["txt"].ToString();
}

how to use session in ASP.Net with C#

how to use session in ASP.Net with C#

learn more about Response.Redirect() and Server.Transfer() in ASP.Net
What is Context Object in ASP.Net

I hope you will enjoy this asp.net tutorial about session variables asp.net.

Leave a Reply

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

2 + 6 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">