1 / 18

ADO.Net DataSet Objects Demos

ADO.Net DataSet Objects Demos. DataSet Object. A DataSet object can hold several tables and relationships between tables.

didina
Download Presentation

ADO.Net DataSet Objects Demos

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ADO.Net DataSet Objects Demos

  2. 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.

  3. 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.

  4. DataSet and Related Objects DataSet Tables DataView DataAdapter Commands Connection DataSource

  5. Structure of a Dataset Dataset Tables Data table Rows Data Row Columns Data Column Constraints Relations Constraint Data Relation

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

  7. 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.

  8. 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

  9. 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.

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

  11. 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

  12. 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

  13. 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

  14. 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

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

  16. Example • Create a web form with a radiobuttonlist to choose rating and display customers with the selected rating in a data grid.

  17. 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

  18. 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

More Related