ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.

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.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
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.
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.
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.
Updating Database. Two Approaches to Update Database 1. Using the DataSet object, the updates are first applied to the DataSet then applied to the database.
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.
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.
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.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
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.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
HNDIT Rapid Application Development
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
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
ADO.NET Framework.
ASP.NET and ADO.NET.
Coding ADO.NET Objects: Connection, Command, DataReader
Chapter 10 Accessing Database Files
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Command Object’s ExecuteNonQuery Method
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

ASP.NET and ADO.NET

ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database

Bind the DataReader to a Server Control DataGrid: –dim objDataReader as oledbDataReader –objDataReader=objComm.executeReader() –dgCustomer.datasource=objDataReader –dgCustomer.DataBind() The Datagrid is defined as: – Demo: CommandReadCust.ASPX

Select Rating from a RadiobuttonList Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where rating = '" & RadioButtonList1.SelectedItem.Value & "'" Dim objComm As New OleDbCommand(strSQL, objConn) Dim Results As String objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() DataGrid1.DataSource = objDataReader DataGrid1.DataBind() End Sub

ListControl Base Class DropDownList, ListBox, CheckBoxList, RadioButtonList Properties: –AutoPostBack –DataSource –DataTextField: The field in the datasource that provides the text to be used for the list items. –DataValueField –SelectedItem Note: Value of the selected item: SelectedItem.Value Event: OnSelectedIndexChanged

Some Possible Causes of Error The procedure in the Page_Load event is executed everytime the web page is loaded. –1. The AutoPostBack property must set to True for the OnSelectedIndexChanged event to work. –2. If the ListItems are created by the Items.Add method, it may be duplicated. –3. The ADO.Net objects and binding do not need to be recreated every time. If not page.isPostBack then … Demo: ASPNET/ListBoxPostback.aspx

Binding a DataReader to a DropDownList with PostBack Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select cid from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() ListBox1.DataSource = objDataReader ListBox1.DataTextField = "CID" ListBox1.DataBind() End If End Sub Note: ListBox PostBack property is set to True.

Display Selected Customer in dataGrid Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid = '" & ListBox1.SelectedItem.Value & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() DataGrid1.DataSource = objDataReader DataGrid1.DataBind() End Sub

Use List Items’ Add Method to Create a DropDownList from a DataReader do while objDataReader.Read()=true Cnamelist.items.add (objDataReader("Cname")) loop

Demo:ReaderUpdate.Aspx Use a DataReader to create a dropwdownList with customer names.. Use a second DataReader to bind the selected customer data to a datagrid. Let user to choose a new customer rating from a listbox. Update customer’s rating using Command object’s ExecuteNonQuery method. Create another reader to bind the selected customer’s new data in a second grid.

DataSet and Related Objects DataSet: Can contain multiple tables and relationships. DataTable object: Represents a table in the dataset. DataAdapter: This the object used to pass data between the database and the dataset. The Fill method copies the data into the dataset, and the Update method copies the updates back into the database. DataView: This represents a specific view of the DataTables held in the dataset.

Reading Data into a Table dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from customer;" dim objDataSet as new Dataset() dim objAdapter as new OledbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust")

Binding a Table to a Server Control Each Table object has a DefaultView property. Create a DataView object for the table, then use the DataView object to bind the control. objAdapter.Fill(objDataSet, "Cust") dim objDataView as new DataView(objDataSet.tables("Cust")) dgCustomer.Datasource=objDataView dgCustomer.DataBind() Demo: AdapterReadCust.Aspx

DataView Object The DataView object exposes a complete table or a subset of the records from a table. Properties: –RowFilter –objDataView.rowfilter="rating='" & Ratinglist.SelectedItem.Text & "'“ –Demo: AdapterReadcustView.aspx, AdapterReadcustView2.aspx –AdapterReadcustView3.aspx

Creating Multiple Tables in A DataSet dim strSQLCust as string = "select * from customer;" dim strSQLOrder as string ="select * from orders;" dim objComm as new OledbCommand() dim objAdapter as new OledbDataAdapter() dim objDataSet as new Dataset() objComm.Connection=objConn objComm.CommandType=COmmandType.Text objComm.CommandText=strSQLCust objAdapter.SelectCommand=objComm objConn.open() objAdapter.Fill(objDataSet, "Customer") objComm.COmmandText=strSQLOrder objAdapter.Fill(objDataSet, "Orders") Demo: AdapterCustOrder.aspx

Adding Relationship to a Dataset The Dataset object has a Relations property. It is a collection of DataRelations. We can use a relationship to enforce the referential integrity. To define a DataRelation: –DataRelObj=DataRelation(RelationName, ParentTable Field, ChildTableField) objRel = New DataRelation("custOrder", objDataset.tables("customer").columns(" cid"), objDataset.tables("orders").columns("ci d"))

DataTable’s Rows Property This is a collection of all the records in a table, a collection of DataRow objects. The DataRow object has a GetChildRows method that returns a collection of rows from another table that are related as child rows to this row.

Access Rows in a DataRow Collection dim objTable as DataTable = objDataset.Tables("Customer") dim objRow as DataRow For each objRow in objTable.Rows strResult=strResult+" " & objRow("cid") & " " & objRow("cname") & " " Next

Displaying Parent/Child Records in a Relation Define the relation. Specify the relation in the GetChildRows method of the DataRow object. Demo: ParentChild.Aspx, ParentChildTable.aspx

Persistence of Data between Page Postback Each page is entirely new each time we submit information to the same page. There is no persistence of information between pages (other than the ViewState which remembers the data contained in controls). Variables and ADO objects are lost. We can store variables and ADO objects in Session or Application. Demo: StateTestCounter.ASPX, PersistDataSet.Aspx

Increase Counter by One. What is wrong? dim counter as integer sub Page_Load() counter=0 end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) end sub Demo: StateTestCounter.ASPX

Save Counter in Session Demo: StateTestCounter2.ASPX dim counter as integer sub Page_Load() if not page.isPostBack then counter=0 session("MyCounter")=counter else counter=session("MyCounter") end if end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) session("MyCounter")=counter End sub

sub Page_load() if not page.ispostback then strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" objConn.connectionString=strConn strSQL = "select * from customer;" dim objAdapter as new OledbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust") objDataView=objDataSet.Tables("cust").DefaultView dgCustomer.Datasource=objDataView dgCustomer.DataBind() response.write ("this is the first grid") Session("MyDataSet")=objDataSet end if end sub sub ClickHandler(Sender As Object, E As EventArgs) objDataSet=Session("MyDataSet") objDataView=objDataSet.tables("Cust").defaultView dgCustomer2.Datasource=objDataView dgCustomer2.DataBind() response.write("this is ClcikHandler") end sub

The Effects of PostBack on Bound Controls If the databinding is done in a PageLoad event procedure without checking postback, the databinding is repeated and the control is initialized to its original state. For list controls, such as ListBox, CheckboxList, and Radiobuttonlist, the selected item is no longer selected.

Demo Dim fruits As New ArrayList() Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'If Not Page.IsPostBack Then fruits.Add("apple") fruits.Add("orange") fruits.Add("banana") CheckBoxList1.DataSource = fruits CheckBoxList1.DataBind() 'End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer For i = 0 To CheckBoxList1.Items.Count - 1 If CheckBoxList1.Items(i).Selected Then Response.Write("you check" & CheckBoxList1.Items(i).Text) End If Next End Sub

Binding to an ArrayList (Collection) of Objects Dim emps As New ArrayList() Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim myEMp1 As New Emp() myEMp1.eid = "e1" myEMp1.ename = "peter" myEMp1.salary = 5000 Dim myEmp2 As New Emp() myEmp2.eid = "e2" myEmp2.ename = "paul" myEmp2.salary = 6000 emps.Add(myEMp1) emps.Add(myEmp2) RadioButtonList1.DataSource = emps RadioButtonList1.DataTextField = "eid" RadioButtonList1.DataValueField = "salary" RadioButtonList1.DataBind() Session("MyEmps") = emps End If End Sub Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged emps = Session("MyEmps") Response.Write(emps.Item(RadioButtonList1.SelectedIndex).ename) Response.Write(emps.Item(RadioButtonList1.SelectedIndex).salary.ToString) End Sub