Category Archives: ASP.Net Objects

How to Set Session Timeout in ASP.Net using web.config.

In this asp.net post, we will learn how to set session timeout property in asp.net using web.config file.

Session is a length/period of time that a particular browser instances spends accessing a website.

The Session object is used to persist data across a user session during the user’s visit to a website.

In asp.net the by default session timeout = 20 minutes, but in some cases we need to change session time increment or decrement by changing web.config file setting.

here, we have set session timeout 1 minute in web.config.

 

Here, if we use session object then the session will expire after 1 minute automatically.

let’s understand with an example how to expire session and which kind of error generate while session expire.

Here, we design two web forms and send value from one web forms to second web forms using session variable.
and we set the session expire time to 1 minute in web.config file. if our second page goes in idle position up to one minute, after 1 minute we will get session object expire error on second page.

STEP 1 : Create ASP.Net web application.

after creating new web application add new web forms in asp.net application.

Design the asp.net web for like below :

Set Session timeout in asp.net using web.config

Set Session timeout in asp.net using web.config

Here, we design a web forms for input some value and send text value to second page by clicking the send button.
the send button code is:

In above code, we first store TextBox1 value to Session[“val”] object and the redirect page to sec.aspx page by clicking send button.

Set Session timeout in asp.net using web.config

Set Session timeout in asp.net using web.config

In above forms we have one button to get the session value and one label for display the session value.

The get value button code is :

Now, If our web form stays idle more then one minute continuously, then after you will try to get session value, you will get below error message.

Set Session timeout in asp.net using web.config

Set Session timeout in asp.net using web.config

Set Session timeout in asp.net using web.config

Set Session timeout in asp.net using web.config

I hope this asp.net session timeout example will help you…..

How to use Context Object in ASP.Net C#

The ASP.Net Context object is the same as the Session Object as we learnt previous asp.net post.

The Context Object is used to store the Value and Send it to the other page in ASP.Net.

The main Difference between Context and Session is the Context Object will be null when we send page to server.
That means we we use Context Object we must use Server.Transfer Method to redirect user to other page.

If we use Response.Redirect the Context value will be null on other page.

we can use Response.Redirect and Server.Transfer with Session Object in ASP.Net, but Response.Redirect cant use with Context Object in ASP.Net.

 

The Syntax of Context Object in ASP.Net

Declare the Context Object in ASP.Net

Context.Items[“Id”]=value;
Server.Transfer(“NextPage.aspx”);

 

Retrieve the Context Value on NextPage in ASP.Net

string myvalue = Context.Items[“Id”].ToString();
Response.Write(“Your Value = “+ myvalue);

 

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

 

Here we tack an Example of Context Object in ASP.Net.

First Create a ASP.Net website and create two web form and Design the web form like:

How to use Context Object in ASP.Net C#

How to use Context Object in ASP.Net C#

Write the Below code on SEND Button click event in ASP.Net

protected void Button1_Click(object sender, EventArgs e)
{
Context.Items[“txt”] = TextBox1.Text;
Server.Transfer(“answer.aspx”);
}

 

How to use Context Object in ASP.Net C#

How to use Context Object in ASP.Net C#

Write below code on Page_Load Event of other page where we want display a value of Context object.

protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = Context.Items[“txt”].ToString();
}

 

How to use Context Object in ASP.Net C#

How to use Context Object in ASP.Net C#

learn about asp.net session timeout ?

I hope this asp.net Example of Context object will help you……

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.

Difference between Response.Redirect() and Server.Transfer() in ASP.Net

What is main Difference between Response.Redirect() and Server.Transfer() method in ASP.Net C#.

The Both Method used to redirect user to new page in ASP.Net.

Response and Server both are the object of ASP.Net Technology.

If we want to go from one web form to other web from in asp.net we can use both Response.Redirect and Server.Transfer.

Response.Redirect(“Pagename.aspx”)

We can use response.redirect to send user to same server or another server.

If we use Response.Redirect method we can finish the whole Round trip and then after send user to another page.
we can use Session()  object with Response.Redirect in ASP.Net
We can not use Context object with Response.Redirect because context object value will be black while page goes to server.

Resonse.redirect can be used both for aspx and html pages.

Response.Redirect(“meera.aspx”);
Response.Redirect(“meera.html”);

In Response.Redirect the browser’s history is updated.

 

Server.Transfer(“Pafename.aspx”)

we can use Server.Transfer to send user to same server web form, we can not send user to other server.

we can use Context with Server.Transfer to send value from one form to another form.

server.transfer is only used for aspx pages it will not work for html pages.
Server.Transfer(“meera.aspx”);

In  Server.Transfer the browser’s history is not updated.

 

What is Context Object in ASP.Net

What is Session Object in ASP.Net