How to use ApplicationState or Global.asax file.
How to use ApplicationState or Global.asax file.
Application object is an object of httpApplicationState class.whenever the information is common to several pages of website than we go for applicationstate object to show that information . may be that information is count user login or any display record which is common to all.
global.asax is carrying events, some event is start users and some event is start for application .there are several events to specific work.
there are session_start and session_end events, session_start event works like a constructor(session_start (login user) ) and session_end event works like a destructor (session_end(logout user).
if one user requested 3 times than how many time session start event fire...? .... only one time behave like constructor.
if there are 15 users login or hit the website than session object 15 times made for every users.
*** application_start event and application_end event fires only one times .
** if we want to know , how many time the wesite is visited than we use the applicationstate .
Global.asax
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["temp"] = 0;
// Application["mycon"] = "initial catalog=practicecac; data source=om-pc; integrated security=yes";
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application["ctr"] = Convert.ToInt32( Application["ctr"] )+ 1;
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application["ctr"] = Convert.ToInt32(Application["ctr"]) - 1;
}
</script>
Page.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Application["ctr"].ToString();
//SqlConnection con = new SqlConnection(Application["mycon"].ToString());
//SqlCommand cmd = new SqlCommand("select * from tbl_StudentRecords",con);
//con.Open();
//SqlDataReader dr = cmd.ExecuteReader();
//dr.Read();
//Label1.Text=dr[1 ].ToString();
//con.Close();
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home