Sponsored Links
ODP.NET Tutorials
- Getting Started with Oracle and ODP.NET
- ODP.NET - Fundamental ODP.NET Classes to Retrieve Data
- ODP.NET - Retrieving Data Using OracleDataReader
- ODP.NET - Retrieving Multiple Rows on to the Grid
- ODP.NET - Retrieving Typed Data
- ODP.NET - Filling a DataTable Using OracleDataReader
- ODP.NET - Retrieving a Single Row of Information Using OracleDataAdapter
- ODP.NET - Working with DataTableReader
- ODP.NET - Populating a Dataset with a Single Data Table
- ODP.NET - Populating a Dataset with Multiple Data Tables
- ODP.NET - Presenting Master-Detail Information Using a Dataset
- ODP.NET - OracleCommand Object
- ODP.NET - Handling Nulls when Executing with ExecuteScalar
- ODP.NET - Handling Nulls when Working with OracleDataReader
- ODP.NET - Working with Bind Variables together with OracleParameter
- ODP.NET - Working with OracleDataAdapter with OracleCommand
- ODP.NET - Techniques to Improve Performance while Retrieving Data
Tutorials
ODP.NETODP.NET - Presenting Master-Detail Information Using a Dataset
ODP.NET - Presenting Master-Detail Information Using a Dataset
As mentioned before, a DataSet object can have its own relations between data tables existing in it. We can add these relations dynamically at the client side (within an application), to represent master-detail (or hierarchical) information. The following code gives the list of employees (in the bottom grid) based on the department you choose in the top grid:
- Imports Oracle.DataAccess.Client
- Public Class Form8
- Private Sub btnData_Click(ByVal sender As
- System.Object, ByVal e As System.EventArgs) Handles
- btnData.Click
- 'create connection to db
- Dim cn As New OracleConnection("Data Source=xe; _
- User Id=scott;Password=tiger")
- Try
- Dim ds As New DataSet
- Dim adp As OracleDataAdapter
- adp = New OracleDataAdapter("SELECT deptno,
- dname, loc FROM Dept", cn)
- adp.Fill(ds, "Departments")
- adp.Dispose()
- adp = New OracleDataAdapter("SELECT empno, ename,
- job, mgr, hiredate, sal, comm, deptno FROM
- Emp", cn)
- adp.Fill(ds, "Employees")
- adp.Dispose()
- ds.Relations.Add(New DataRelation("FK_Emp_Dept",
- ds.Tables("Departments").Columns("Deptno"),
- ds.Tables("Employees").Columns("Deptno")))
- Dim bsMaster As New BindingSource(ds, _
- "Departments")
- Dim bsChild As New BindingSource(bsMaster, _
- "FK_Emp_Dept")
- Me.DataGridView1.DataSource = bsMaster
- Me.DataGridView2.DataSource = bsChild
- Catch ex As Exception
- 'display if any error occurs
- MessageBox.Show("Error: " & ex.Message)
- 'close the connection if it is still open
- If cn.State = ConnectionState.Open Then
- cn.Close()
- End If
- End Try
- End Sub
- End Class
Once the DataSet is filled with data tables (Departments and Employees), we can add an in-memory relation using the following statement:
ds.Relations.Add(New DataRelation("FK_Emp_Dept",
ds.Tables("Departments").Columns("Deptno"),
ds.Tables("Employees").Columns("Deptno")))
The above statement simply adds a new relation (named FK_Emp_Dept) between two DataTable objects (Departments and Employees) based on the column Deptno (available in both DataTable objects).
To present the information in a master-detail fashion, we can make use of the BindingSource object as follows:
Dim bsMaster As New BindingSource(ds, "Departments")
Dim bsChild As New BindingSource(bsMaster, "FK_Emp_Dept")
In the above code fragment, we used two BindingSource objects corresponding to master and child data tables respectively. The child BindingSource object is created based on the master BindingSource object together with the specification of DataRelation. Once the BindingSource objects are ready, we can assign them as data sources to the DataGridView controls as following:
Me.DataGridView1.DataSource = bsMaster
Me.DataGridView2.DataSource = bsChild
The output for the above code would look similar to the following figure:

You can observe that this screen displays only the employees working in department number 20 as that is selected in the top grid.
Comments
Sponsored Links
