Working with the Application Object. Session VS Application Object The Session object helps to preserve data on a per user basis. What if we want to initialize.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
CIS 451: ASP Sessions and Applications Dr. Ralph D. Westfall January, 2009.
Introduction to ASP.Net ISYS 512. ASP.NET ASP.NET is a server-side technology for creating dynamic web pages. ASP.NET allows you to use a selection of.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
Introduction to ASP.Net
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Coding ADO.NET Objects: Connection, Command, DataReader.
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
Website Security ISYS 512. Cookies Data in Cookies System.Web Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
SQL Azure Database Windows Azure SQL Database is a feature-rich, fully managed relational database service that offers a highly productive experience,
Overview of Previous Lesson(s) Over View  Server controls are small building blocks of the graphical user interface, which includes  Text boxes  Buttons.
Welcome  30 Question & 30 Minutes  Question Will Move Automatically After 1 Min Your Exam Start Now Lecturer : Zalak Thakrar.
CIS 375—Web App Dev II ASP II. 2 ASP Session: Introduction The Session _______ is used to store information about, or change settings for a user session.
Session 10: Managing State. Overview State Management Types of State Management Server-Side State Management Client-Side State Management The Global.asax.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Managing State.
ASP The Global.asa file Y.-H. Chen International College Ming-Chuan University Fall, 2004.
Working with GridView Control: Adding Columns. Adding Buttons to a Bound GridView: 1. Drag the WebProduct table from Data connection to a page 2. Demo.
ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level.
Session and Cookie Management in.Net Sandeep Kiran Shiva UIN:
Building Applications using ASP.NET and C# / Session 15 / 1 of 17 Sessio n 15.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Module 7: Creating a Microsoft ASP.NET Web Application.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
Command Object’s ExecuteNonQuery Method ISYS 512.
1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database.
Lecture Note 8: ASP Including Files and The Global.asa file.
1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End.
Working With ASP.NET Application. Create a new virtual directory The procedure to create a new virtual directory Internet Services Manager Right click.
Building Applications using ASP.NET and C# / Session 5 / 1 of 19 Session 5.
Coding ADO.NET Objects: Connection, Command, DataReader.
Active Server Pages Session - 3. Response Request ApplicationObjectContext Server Session Error ASP Objects.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
 ASP.NET provides an event based programming model that simplifies web programming  All GUI applications are incomplete without enabling actions  These.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
ASP.NET State Management Overview Prepared By Manish Kumar Aery(IM66) Department of computer Application IET Bhaddal (Ropar)
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Coding ADO.NET Objects: Connection, Command, DataReader
Session Variables and Post Back
ASP.NET and ADO.NET.
Session management.
CS 3870 Prog5 Shopping Bag.
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Shopping Cart Demo.
Command Object’s ExecuteNonQuery Method
Presentation transcript:

Working with the Application Object

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.

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); }

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

Adding the Global.asax file: Project/Add/New item

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.

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.

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());

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; }

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(); }

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

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(); }

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(); }

Maintaining Visitor Counter Database table: VcounterTable Fields: –TimeStamp, nchar(50) –VisitorCounter, int

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(); }

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(); }