You are on page 1of 8

Introduction

Session and Application are very important in ASP.NET. When we move from one
page to another page, the values of the previous page will get lost, If we want to
hold the previous values, for that purpose we can use session level variale or
application level variales.
Session level variable : !alue will persist till the close of the rowser.
Example : "irst of all we will ta#e a utton and te$t o$ from the toolo$ on we
form li#e the elow image.
then we will add code on the utton clic#.
using System.We%
using System.We.&I%
using System.We.&I.We'ontrols%
namespace session
(
pulic partial class )*efault + System.We.&I.Page
(
protected void Page),oad-o.ect sender, EventArgs e/
(
0
protected void 1utton2)'lic#-o.ect sender, EventArgs e/
(
Session34username45 6 Te$t1o$2.Te$t%
7esponse.7edirect-4We"orm2.asp$4/%
0
0
0
Now we will add a new we form -We"orm2.asp$/
Solution E$plorer897ight clic#89add89new item89We "orm89add.
Now create a lael on this we form, then add the following code on we form
loading.
using System%
using System.'ollections.:eneric%
using System.,in;%
using System.We%
using System.We.&I%
using System.We.&I.We'ontrols%
namespace session
(
pulic partial class We"orm2 + System.We.&I.Page
(
protected void Page),oad-o.ect sender, EventArgs e/
(
,ael2.Te$t 6 4<ost welcome <r.=<rs884 >
Session34username45.ToString-/%
0
0
0
Now run the application -Press "?/. Then enter the value in te$to$.
Then clic# the o# utton.
Application Level variable : Value will persist till the down of we server.
Global.asax file : :loal.asa$ file is ASP.NET application file. :loal.asa$ is
e$tension of :loal Application 'lass. :loal.asa$ file resides in the IIS virtual root of
an ASP.NET application. It is used to declare application level variale. There are
many events in :loal.asa$ file which are as follows+
Application_Init: "ires when the application initiali@es for the first time.
Application_Start: "ires the first time an application starts.
Session_Start: "ires the first time when a userAs session is started.
Application_Begine!uest: "ires each time a new re;uest comes in.
Application_Ende!uest: "ires when the re;uest ends.
Application_Aut"enticatee!uest: Indicates that a re;uest is ready to e
authenticated.
Application_Error: "ires when an unhandled error occurs within the application.
Session_End: "ires whenever a single user Session ends or times out.
Application_End: "ires when the application ends or times out.
eferences : 4http+==en.wi#ipedia.org=wi#i=:loal.asa$4
Example : BIT counter
Solution E$plorer897ight clic#89add89new item89:loal application class
-:loal.asa$/89add li#es elow image.
Then coding on :loal.asa$ file.
using System%
using System.'ollections.:eneric%
using System.,in;%
using System.We%
using System.We.Security%
using System.We.SessionState%
namespace Application
( pulic class :loal + System.We.BttpApplication
(
protected void Application)Start-o.ect sender, EventArgs e/
(
Application34hits45 6 C%
0
protected void Session)Start-o.ect sender, EventArgs e/
(
Application34hits45 6 IntDE.Parse-Application34hits45.ToString-// > 2%
0
protected void Application)1egin7e;uest-o.ect sender, EventArgs e/
(
0
protected void Application)Authenticate7e;uest-o.ect sender, EventArgs e/
(
0
protected void Application)Error-o.ect sender, EventArgs e/
(
0
protected void Session)End-o.ect sender, EventArgs e/
(
0
protected void Application)End-o.ect sender, EventArgs e/
(
0
0
0
Now we will ta#e a utton on we form then coding on utton clic#.
using System%
using System.'ollections.:eneric%
using System.,in;%
using System.We%
using System.We.&I%
using System.We.&I.We'ontrols%
namespace Application
(
pulic partial class )*efault + System.We.&I.Page
(
protected void Page),oad-o.ect sender, EventArgs e/
(
0
protected void 1utton2)'lic#-o.ect sender, EventArgs e/
(
7esponse.Write-4Fh29Gour are user Numer8894 >
Application34hits45.ToString-//%
0
0
0
Now run application -Press "?/. Then clic# the utton.
Again run application then clic# utton.
So when we run the application again and again the numer of views will e
increment according to running mode of we application.
Summar#
So session level variale or application level variale are used for the persist value till
the close of rowser or server.

You might also like