Microsoft .Net Framework 2.0 provides us many ways in solving problems. As you know DataAdapter is one of the efficient instances in fetching record and populating DataSet. It provides disconnected connectivity with database. You can say that DataAdapter servers as a bridge between DataSet and datasource. Whereas, DataRelation object contained in the DataRelationCollection, DataRelation is used to relate two DataTable objects through data column.
Once the relationship is created ConstraintCollection controls that relation should not violate any constraint means foreign key constrain. DataRelationCollection maintains the relationship by disallowing any changes that is performing any invalidate operation. You can access DataRelation objects using Relations property of the Dataset. In this example, I will not perform any kind of update, delete or relational operations.
Here I simply get the record from database fill DataSet, use DataRelation object to create relationship between two tables and finally display record in TreeView control. Web form control contains only two controls one is label control and the second one is TreeView control:
<asp:Label ID="lblMessage" runat="server" Text="Displaying Data Using DataRealtion Object: " /><br /><br />
<asp:TreeView ID="tvDisplayRecord" runat="server" />
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DBConn.Open()
'Create DataAdaptor for Customers and Orders
'Table and fetch record from the Database
Dim DBCustAdap As New SqlDataAdapter("Select * From Customers", DBConn)
Dim DBOrdAdap As New SqlDataAdapter("Select * From Orders", DBConn)
'Create DataSet object and fill DataAdapter
Dim DSCustOrder As DataSet = New DataSet
DBCustAdap.Fill(DSCustOrder, "Customers")
DBOrdAdap.Fill(DSCustOrder, "Orders")
'Add Realation using Relation Attribute
DSCustOrder.Relations.Add("CustomerOrders", DSCustOrder.Tables("Customers").Columns("CustomerID"), DSCustOrder.Tables("Orders").Columns("CustomerID"))
'Fill the TreeView control Nodes using For Loop
For Each PRow As DataRow In DSCustOrder.Tables("Customers").Rows
Dim PNode As New TreeNode(PRow("ContactName"))
Me.tvDisplayRecord.Nodes.Add(PNode)
For Each CRow As DataRow In PRow.GetChildRows("CustomerOrders")
PNode.ChildNodes.Add(New TreeNode(CRow("OrderID")))
Next
PNode.CollapseAll()
'Close Connection and Dispose all database objects
DBCustAdap.Dispose()
DBCustAdap = Nothing
DBOrdAdap.Dispose()
DBOrdAdap = Nothing
DBConn.Close()
DBConn = Nothing
End Sub
Posted on 4/13/2008 1:15:27 AM by hitesh nirmal
Posted on 5/9/2008 3:36:41 AM by Ayesha rana
Posted on 2/3/2010 2:59:26 AM by kamza
Posted on 3/27/2010 7:34:18 AM by sajan
Posted on 4/3/2010 4:06:29 AM by rakesh
Posted on 5/18/2010 3:51:44 PM by Ajit Kumar
Posted on 8/14/2010 12:23:49 AM by hirak
Posted on 9/28/2010 6:42:58 AM by Ole