Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to populate TreeView control using DataAdapter and DataRelation Objects
Download Source Code :

In this article I will try to explain you the technique of populating TreeView control using DataAdapter and DataRelation objects. Here I have created a sample application in ASP.Net 2.0.

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" />

To get the record from SQL database Import two files System.Data and System.Data.SqlClient. Declare globally database connection string. On the Page_Load event add the following code:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



'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"))



For Each CRow As DataRow In PRow.GetChildRows("CustomerOrders")

PNode.ChildNodes.Add(New TreeNode(CRow("OrderID")))








'Close Connection and Dispose all database objects


DBCustAdap = Nothing


DBOrdAdap = Nothing


DBConn = Nothing


End Sub

Article Comments
i need to count the child node when i enter the child node id and show on the treeview

if you have any solutions plz send me

Posted on 4/13/2008 1:15:27 AM by hitesh nirmal

I have resolved my problem by your article. Thanks

Posted on 5/9/2008 3:36:41 AM by Ayesha rana

Thanks for your article but still I have a problem on setting navigationurl and targetframe for the developed child node.

Your help please

Posted on 2/3/2010 2:59:26 AM by kamza

hey thanks man ....u saved my day.......

Posted on 3/27/2010 7:34:18 AM by sajan

i have create a treeview it run successful but i can not create a chield node to another child node please give a solution in this problem

Posted on 4/3/2010 4:06:29 AM by rakesh

It is a Perfect Code Thanks A Lote,

Posted on 5/18/2010 3:51:44 PM by Ajit Kumar

thanks for the code

Posted on 8/14/2010 12:23:49 AM by hirak

Awesome article.

Posted on 9/28/2010 6:42:58 AM by Ole

Add Article Comment:
Name :
Email Address :
Comments :
<< Working with System.Collection Namespace

Disclaimer - Privacy
© 2002-2017