Download presentation
Presentation is loading. Please wait.
Published byEsther Opal Gilmore Modified over 9 years ago
1
Working with the Application Object
2
Session VS Application Object The Session object helps to preserve data on a per user basis. What if we want to initialize variables that are available for all users? This means that a change in the value of an application variable is reflected in the current sessions of all users. For example, we may like to fix variables like tax rate, discount rate, company name, etc., that will be specified once for all variables we can access in a session. This is where application variables come in.
3
Saving Data in Application and Retrieving Data From Application protected void Page_Load(object sender, EventArgs e) { double drate=.045; string companyName="ABC Company"; Application["discountRate"] = drate; Application["companyName"] = companyName; } protected void Button1_Click(object sender, EventArgs e) { double discountRate; string companyName; discountRate=(double) Application["discountRate"]; companyName=(string) Application["companyName"]; Response.Write(discountRate.ToString() + companyName); }
4
Global.asax File The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events. Application events: –Application_Start –Application_End Session events: –Session_Start –Session_End
5
Adding the Global.asax file: Project/Add/New item
6
Application_Start Application_Start event gets triggered when the first request for any resource in the application comes. Resource can be a page or an image in the application. When the very first request for a resource, say a web page, is made by a user “Application_Start” is triggered after which this event is not at all executed. If by any chance the server where the application is hosted is restarted then this event is fired once again i.e. when the very first request for any resource in the application is made after the server is reset.
7
Session_Start Session start event is fired only when a new session for a user starts. Once “Session_Start” for a user is fired then if the user makes subsequent request to any resource within the application this event is not at all triggered. This event can be used when you want to do something when the user visits you site/application for the first time or when his session starts.
8
Example: Create a variable to show current date and save it in Session protected void Session_Start(object sender, EventArgs e) { string CurrentDay = "Today is " + System.DateTime.Now.DayOfWeek.ToString() + ", " + System.DateTime.Now.ToString(); Session["CurrentDay"] = CurrentDay; } This variable can be accessed from any page: Response.Write(Session["CurrentDay"].ToString());
9
Example: Saving a variable in Application and a dataset in Session protected void Application_Start(object sender, EventArgs e) { double todayRate =.045; Application["todayRate"] = todayRate; } DataSet objDataSet = new DataSet(); protected void Session_Start(object sender, EventArgs e) { string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from customer;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; }
10
The saved dataset can be accessed from other page protected void Page_Load(object sender, EventArgs e) { double todayRate; todayRate = (double)Application["todayRate"]; Response.Write("today's rate is: " + todayRate.ToString("p")); DataSet myDS = (DataSet)Session["myDataset"]; GridView1.DataSource = myDS; GridView1.DataMember = "Customer"; GridView1.DataBind(); }
11
Application/Session Demo When are Application and Session events triggered? Database: –WebLog table: StartEnd field, text TimeStamp field, text Session.abandon(); –Trigger Session_End event Application.Lock(); Application.Unlock(); In Azure, Application_End event will be triggered when a website is stopped
12
Global.Asax file string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; protected void Application_Start(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('ApplicationStart','" + timeStamp + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); } protected void Session_Start(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('SessionStart','" + timeStamp + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); }
13
protected void Session_End(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('SessionEnd','" + timeStamp + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); } protected void Application_End(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('ApplicationEnd','" + timeStamp + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); }
14
Maintaining Visitor Counter Database table: VcounterTable Fields: –TimeStamp, nchar(50) –VisitorCounter, int
15
Maintaining Visitor Counter string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlDataReader objDataReader; protected void Application_Start(object sender, EventArgs e) { string strSQL = "select max(VisitorCounter) from VCounterTable"; SqlCommand objComm = new SqlCommand(strSQL, objConn); objConn.Open(); Application["visitorCounter"] = objComm.ExecuteScalar(); objConn.Close(); } protected void Session_Start(object sender, EventArgs e) { Application.Lock(); Application["visitor"] = (int) Application["visitor"] + 1; Session["visitor"] = Application["visitor"]; Application.UnLock(); }
16
protected void Application_End(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQL = "Insert Into VCounterTable Values ('" + (DateTime.Now.ToString()) + "', " + Application["visitorCounter"] + ");"; SqlCommand objComm = new SqlCommand(strSQL, objConn); objConn.Open(); objComm.ExecuteNonQuery(); objConn.Close(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.