Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.

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.
Cookies. Data in Cookies Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One or more pieces of data Keys Define.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
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.
Introduction to ADO.NET Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
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 Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ASP.Net
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
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.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
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.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
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.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
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.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
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.
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.
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.
ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
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.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
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.
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.
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.
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
Introduction to ADO.NET
Shopping Cart Demo.
Command Object’s ExecuteNonQuery Method
Presentation transcript:

Working with Session and Application Objects

Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.

Persistence of Data between Page Postback We can store variables and ADO objects in Session or Application object.

Increase Counter by One. What is wrong? Dim counter As Integer Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click counter = counter + 1 Response.Write(counter) End Sub

Save Counter in Session int counter=0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) Session["mYcounter"]=counter; else counter = (int) (Session["mYcounter"]); protected void Button1_Click(object sender, EventArgs e) { counter += 1; Session["mYcounter"] = counter; Response.Write(counter); } Note: Can we do this in the Page_Load event: if (Page.IsPostBack) counter = (int) (Session["mYcounter"]);

DataSet Example Create a web form with a Radiobuttonlist to choose rating and display customers with the selected rating in a data grid. RadioButtonList control: –Items property –SelectedValue property

DataSet objDataSet = new DataSet(); DataView objDataView = new DataView(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; } else objDataSet = (DataSet) Session["myDataset"]; } protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { objDataView = objDataSet.Tables["customer"].DefaultView; objDataView.RowFilter = "rating = '" + RadioButtonList1.SelectedItem.ToString() + "'"; GridView1.DataSource = objDataView; GridView1.DataBind(); }

Binding a ListBox and Display Two other Fields DataSet objDataSet = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; ListBox1.DataSource = objDataSet.Tables["Customer"]; ListBox1.DataTextField = "cid"; ListBox1.DataValueField = "Cname"; ListBox1.DataBind(); } else objDataSet = (DataSet)Session["myDataset"]; } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = ListBox1.SelectedValue; TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToString(); }

A page with CID Listbox to display customer data in textboxes and orders data in GridView. Use one adapter to create a dataset with two tables. Create bound listbox and automatically select the first customer’s CID: – ListBox1.Items[0].Selected = true; GridView control is bound using Orders table’s view.

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); string strSQLOrders = "select * from orders"; objAdapter.SelectCommand.CommandText = strSQLOrders; objAdapter.Fill(objDataSet, "Orders"); Session["myDataset"] = objDataSet; ListBox1.DataSource = objDataSet.Tables["Customer"]; ListBox1.DataTextField = "cid"; ListBox1.DataValueField = "cid"; ListBox1.DataBind(); ListBox1.Items[0].Selected = true; TextBox1.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["Cname"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToString(); DataView ObjDataView= new DataView(); ObjDataView = objDataSet.Tables["Orders"].DefaultView; ObjDataView.RowFilter = "CID='" + ListBox1.SelectedValue + "'"; GridView1.DataSource = ObjDataView; GridView1.DataBind(); Session["myDataset"] = objDataSet; } else objDataSet = (DataSet)Session["myDataset"]; }

Listbox SelectedIndexChanged event protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["Cname"].ToStri ng(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToStrin g(); DataView ObjDataView = new DataView(); ObjDataView = objDataSet.Tables["Orders"].DefaultView; ObjDataView.RowFilter = "CID='" + ListBox1.SelectedValue + "'"; GridView1.DataSource = ObjDataView; GridView1.DataBind(); }

Share Variables between Pages First page: –Session[“CustName”]=“Smith”; Second page: – Response.Write("Welcome, " + Session["CustName“].ToString();)

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

If a Dataset object is needed in many pages, it can be created and saved in the Session protected void Session_Start(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); DataSet objDataSet = new DataSet(); objAdapter.Fill(objDataSet, "Customer"); string strSQLOrders = "select * from orders"; objAdapter.SelectCommand.CommandText = strSQLOrders; objAdapter.Fill(objDataSet, "Orders"); Session["myDataset"] = objDataSet; }

The saved dataset can be accessed from other page protected void Page_Load(object sender, EventArgs e) { DataSet objDataSet = new DataSet(); objDataSet =(DataSet) Session["MyDataset"]; GridView1.DataSource = objDataSet; GridView1.DataMember = "Customer"; GridView1.DataBind(); }

Application/Session Demo When are Application and Session events triggered? Database: –WebLog table: StartEnd field TimeStamp field Session.abandon(); Application.Lock(); Application.Unlock();

Global.Asax file protected void Application_Start(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\WebLogDB.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('ApplicationStart','" + timeStamp + "')"; OleDbCommand objCommInsert= new OleDbCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); } protected void Session_Start(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\WebLogDB.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('SessionStart','" + timeStamp + "')"; OleDbCommand objCommInsert = new OleDbCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); }

protected void Session_End(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\WebLogDB.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('SessionEnd','" + timeStamp + "')"; OleDbCommand objCommInsert = new OleDbCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); } protected void Application_End(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\WebLogDB.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string timeStamp = System.DateTime.Now.ToString(); string strSQLInsert; strSQLInsert = "Insert into WebLog values ('ApplicationEnd','" + timeStamp + "')"; OleDbCommand objCommInsert = new OleDbCommand(strSQLInsert, objConn); objConn.Open(); objCommInsert.ExecuteNonQuery(); objConn.Close(); }

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\SalesDB2011.accdb"; string strSQL; OleDbDataReader objDataReader; protected void Application_Start(object sender, EventArgs e) { OleDbConnection objConn = new OleDbConnection(strConn); strSQL = "select VisitorCounter from VCounterTable where CounterDate = ("; strSQL = strSQL + "Select Max(CounterDate) from VCounterTable);"; OleDbCommand objComm=new OleDbCommand(strSQL, objConn); objConn.Open(); objDataReader = objComm.ExecuteReader(); objDataReader.Read(); Application["visitor"] = objDataReader["VisitorCounter"]; 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) { OleDbConnection objConn = new OleDbConnection(strConn); strSQL = "Insert Into VCounterTable Values (#" + (DateTime.Now.ToString()) + "#, " + Application["visitor"] + ");"; OleDbCommand objComm=new OleDbCommand(strSQL, objConn); objConn.Open(); objComm.ExecuteNonQuery(); objConn.Close(); }