Top Banner
ASP.NET and ADO.NET
26

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

Jan 15, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.

ASP.NET and ADO.NET

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

ADO.NET Objects

Data Set

.NET Applications

Data Reader

Command Object

Connection Object

Managed Data Provider(OLEDB)

Database

Page 3: 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:– <asp:datagrid id="dgCustomer" runat="server"

/><br />

Demo: CommandReadCust.ASPX

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

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

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

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

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

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

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

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.

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

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

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

Use List Items’ Add Method to Create a DropDownList from a DataReader

• do while objDataReader.Read()=true• Cnamelist.items.add (objDataReader("Cname"))

• loop

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

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.

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

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.

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

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")

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

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

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

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

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

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

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

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("cid"))

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

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.

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

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+"<b>" & objRow("cid")

& " " & objRow("cname") & "</b><br />"

• Next

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

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

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

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

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

Increase Counter by One. What is wrong?

<script runat="server">

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

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

Save Counter in SessionDemo: StateTestCounter2.ASPX

<script runat="server">

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

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

sub Page_load()if not page.ispostback then

strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb"

objConn.connectionString=strConnstrSQL = "select * from customer;"dim objAdapter as new OledbDataAdapter(strSQL, objConn)objAdapter.Fill(objDataSet, "Cust")objDataView=objDataSet.Tables("cust").DefaultViewdgCustomer.Datasource=objDataViewdgCustomer.DataBind()response.write ("this is the first grid")Session("MyDataSet")=objDataSet

end ifend sub

sub ClickHandler(Sender As Object, E As EventArgs)objDataSet=Session("MyDataSet")objDataView=objDataSet.tables("Cust").defaultViewdgCustomer2.Datasource=objDataViewdgCustomer2.DataBind()response.write("this is ClcikHandler")end sub

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

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.

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

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

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

Binding to an ArrayList (Collection) of ObjectsDim 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