Presenting Master-Detail Information Using a Dataset

          

Interview questions

Presenting Master-Detail Information Using a Dataset


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; _
UserId=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

 
  Book Excerpt: Retrieving Data from Oracle
  Using ODP.NET
  Chapter Contents

This excerpt from ODP.NET Developer's Guide: Oracle Database 10g Development with Visual Studio 2005 and the Oracle Data Provider for .NET  by Jagadish Chatarji Pulakhandam, Sunitha Paruchuri, is printed with permission from Packt Publishing, Copyright 2007. 


Related Links
>Oracle Interview Questions
>Working with ASP.NET DataList Control
>.Net Framework Interview Questions
>ASP.NET Tutorial
>ASP.NET Interview questions
>Remoting.Net
>ASP.NET Validation Control
>.NET Assembly
>ADO.NET

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.


Page 1 | Page 2 | page 3 | page 4 | page 5 | page 6 | page 7 | page 8



Write your comment - Share Knowledge and Experience


 

Latest placement tests
Latest links
 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring