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

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

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.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
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.
Shopping Cart. ASP.NET State Management Session: User state Application: Application state Cache Static variables: –Public Shared interestRate as Single.
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.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
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.
Shopping Cart. Search and display product information Add item to cart View cart contents Check out.
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 ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
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.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
1 CS387/CS587: Note 08 Shopping Bag DataTable. 2 DataClass Public Shared Function NewShoppingBag() As Data.DataTable Dim bag As New Data.DataTable bag.Columns.Add("Product.
Introduction to XML and Web Service ISYS 512/812.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
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
ASP.NET and ADO.NET.
CS 3870/CS 5870 Test 2 (50 points) Thursday
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

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.

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 Dim counter As Integer Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click counter = counter + 1 Session("MyCounter") = counter Response.Write(counter) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Session("MyCounter") = counter Else counter = Session("MyCounter") End If End Sub

Example: Save DataReader in Sesson Create a data reader Use button to display record one at a time

Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb“ Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) Dim objDataReader As OleDbDataReader Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then objConn.Open() objDataReader = objComm.ExecuteReader() objDataReader.Read() TextBox1.Text = objDataReader("cid") TextBox2.Text = objDataReader("cname") Session("myReader") = objDataReader Else objDataReader = Session("myreader") End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If objDataReader.Read() = True Then TextBox1.Text = objDataReader("cid") TextBox2.Text = objDataReader("cname") Else Label1.Text = "no more record" objConn.Close() End If End Sub

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

Data Binding with DataView And Save the Dataset in Session If Not Page.IsPostBack Then Response.Write(Session("CurrentDay")) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb“ Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Customer") Dim ObjDataView As New DataView() ObjDataView = objDataSet.Tables("Customer").DefaultView GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataSet End If

Selecting a Subset of Records with DataView’s RowFilter Property objDataView.RowFilter = criteria Note: The criteria can be a simple or complex condition.

DataView Example Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged objDataSet = Session("DS") Dim ObjDataView As New DataView() ObjDataView.Table = objDataSet.Tables("Customer") ObjDataView.RowFilter = "rating='" & RadioButtonList1.SelectedValue & "'" GridView1.Visible = True GridView1.DataSource = ObjDataView GridView1.DataBind() End Sub

Binding a ListBox and Display Two Fields Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objdataset, "Customer") ListBox1.DataSource = objdataset.Tables("customer") ListBox1.DataTextField = "CID" ListBox1.DataValueField = "Cname" ListBox1.DataBind() Session("MyDataset") = objdataset Else objdataset = Session("MyDataset") End If End Sub Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedValue TextBox2.Text = objdataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") End Sub

DataTable’s Rows Property This is a collection of all the records in a table, a collection of DataRow objects. DataRow object’s properties and methods.

Accessing a Record with Index objAdapter.Fill(objDataSet, "Customer") TextBox1.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(0) TextBox2.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(1)

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.

Creating Multiple Tables: One Adapter, Many Tables Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) Dim objDataset As New DataSet objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders")

Bind GridView to Orders table’s View Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind()

If Not Page.IsPostBack Then Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders") 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") TextBox2.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataset End If

Listbox’s SelectedIndexChanged Event Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged objDataset = Session("DS") TextBox1.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Cname") TextBox2.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataset End Sub

Share Variables between Pages First page: –Session("CustomerName") = objDataReader("cname") Second page: – Response.Write("Welcome, " + Session("CustomerName"))

Working with Session Start Event in Global.ASAX In Global.ASAX – Public CurrentDay As String – Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) – CurrentDay = "Today is " + Now().DayOfWeek.ToString + ", " + Now().ToString – Session("CurrentDay") = CurrentDay –End Sub In a web page: –Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load – response.Write(session("CurrentDay")) –End Sub