180 likes | 302 Views
ADO.Net DataSet Objects Demos. DataSet Object. A DataSet object can hold several tables and relationships between tables.
E N D
DataSet Object • A DataSet object can hold several tables and relationships between tables. • A DataSet is a set of disconnedted data. Data is extracted from the database and stored in the DataSet object. Updates to the DataSet must copy back to the database to make the changes permanent. • Note: DataSet object belongs to the System.Data name space, not the System.Data.OleDB name space.
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.
DataSet and Related Objects DataSet Tables DataView DataAdapter Commands Connection DataSource
Structure of a Dataset Dataset Tables Data table Rows Data Row Columns Data Column Constraints Relations Constraint Data Relation
Reading Data into a Table • dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" • dim objConn as new OledbConnection(strConn) • dim strSQLSelect as string = "select * from customer;" • dim objDataSet as new Dataset() • dim objAdapter as new OledbDataAdapter(strSQLSelect, objConn) • objAdapter.Fill(objDataSet, "Customer")
Binding GridView with Code Imports System.Data Imports System.Data.OleDb Partial Class Form7 Inherits System.Web.UI.Page 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 Source = c:\salesDB.mdb" 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") GridView1.DataSource = objdataset GridView1.DataMember = "Customer" GridView1.DataBind() End Sub End Class Note: Import the System.Data namespace.
Binding a ListBox with Code 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 objdataset As New DataSet 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() End If End Sub Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedValue End Sub
The effects of Postback • Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values. • Remember the state of the form (data displayed in controls) by adding a hidden _VIEWSTATE variable.
Persistence of Data between Page Postback • We can store variables and ADO objects in Session or Application.
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
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
DataView ObjectConvenient for Binding and Display Records Meeting Criteria • The DataView object exposes a complete table or a subset of the records from a table. • Table’s DefaultView property: • Dim ObjDataView As New DataView() • ObjDataView.Table = objDataSet.Tables("Cust") • Or: ObjDataView = objDataSet.Tables("Cust").DefaultView • DataView can be used as a DataSource in data binding. • Other useful methods: • Find, Sort
Binding GridView with DataView Object Imports System.Data Imports System.Data.OleDb Partial Class Form7 Inherits System.Web.UI.Page 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 Source = c:\salesDB.mdb" 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") DataGridView1.DataSource = objDataSet.Tables("Customer").DefaultView DataGridView1.DataBind() End Sub End Class
Selecting a Subset of Records with DataView’s RowFilter Property • objDataView.RowFilter = criteria • Note: The criteria can be a simple or complex condition.
Example • Create a web form with a radiobuttonlist to choose rating and display customers with the selected rating in a data grid.
Save the Dataset in Session Partial Class form4 Inherits System.Web.UI.Page Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" 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) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then objAdapter.Fill(objdataset, "Customer") Session("myDataSet") = objdataset Else objdataset = Session("MyDataset") End If End Sub
Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Dim myView As DataView myView = objdataset.Tables("Customer").DefaultView myView.RowFilter = "rating='" & RadioButtonList1.SelectedValue & "'" GridView1.DataSource = myView GridView1.DataBind() End Sub End Sub