Click here to Skip to main content
13,089,504 members (62,436 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 16 Nov 2007


, 16 Nov 2007
Rate this:
Please Sign up or sign in to vote.
A DB Base Tree that load it's nodes from a table
Screenshot - LoadTree.jpg


This is a simple code to show how we can load a tree nodes from a table records.

How we use this code

At first step we must create a DataBase and name it "dbTree".
Then we must run this script to create table that reserve our tree nodes. We name this table "tPOI":

USE [dbTree]
/****** Object:  Table [dbo].[tPOI]    Script Date: 11/17/2007 09:32:01 ******/
 [NodeID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
 [ParentID] [numeric](18, 0) NULL,
 [NodeName] [nchar](10) COLLATE Arabic_CI_AS NULL,
 [InUse] <bit> NULL CONSTRAINT [DF_tPOI_InUse]  DEFAULT ((1)),
 [NodeID] ASC

And finally load this records to "tPOI".

NodeID  ParentID          NodeName                InUse
1         0             RootNode1        True
2         1             Child1            True
3         1             Child2            True
4         1             Child3            True
5         2             Child1_1        True
6         2             Child1_2        True
7         0             RootNode2        True

How it works?

Our strategy to load nodes is simple: RootNodes don't have ParentNode so we set their ParentNode Field to Zero ("0") and every child node has a parent node that help us to attach this node to its parent.
Is Simple, isn't?

Now we go to code section:
There is just one function that load records to tree, "treeLoader()".


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood concept but needs improvement Pin
Douglas R. Keesler17-Nov-07 14:50
memberDouglas R. Keesler17-Nov-07 14:50 

I like your concept of loading a tree control from a database, but this is not a good implementation of that concept. It has to work in real world databases. Fieldnames of "Node" and "Parent" have no relevance outside of your demo project.

See, the problem is (other than not being able to use this with our existing databases) what if a user submits a new recipe or article in a new category and now node 1 dynamically becomes node 2. Now you have to update every single record in your databse to reflect the new order -- do you realize what problems this could cause with 200 concurrent connections. And what if I don't want to display EVERY node. What if the user clicks on Desserts and on the next page I only want to display a tree of sub-items under the desserts category. Your control doesn't allow for any of that flexibility.

I would encourage you to keep working on it, and post a new article when you have something that is real world viable.

If I were doing it though I would use a single lookup table with two fields: parent, child. To get the root nodes you would do a lookup for all records where parent = "none" or "root". You would then step to the first root node and query for all records where parent = current node. You would create the resulting values as children of that node. Then you would step down to the first child and query again for all records where parent = current node, etc.. You would recurse all the way stepping through each sibling/child until you got no results, then you would break the recursion, step to the 2nd root node and do the same thing all over again. But you have to make the control a "variable" based control.. Maybe my lookup table is named Nodes, maybe the next guy's is named Categories. Maybe my fields are named "parent/child" and the next guys are named "cat/sub". Maybe I use "none" for root parent, maybe next guy says "root" We need to be able to pass these values (table names, field names, etc) to the script as variables.

I should also be able to pass a starting node (or a root node) to the script and how many levels to show. In other words, if I pass "ASP.NET" to the script, then the 1st level children of the ASP.NET category should be my root level nodes in the resulting tree, etc.. There's also the consideration of how the control will determine what action to take on click events, etc.

Hopefully, these thoughts will give you some ideas.. but work on it and share your results with us. Good luck.

In business, if two people always agree, one of them is unnecessary.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by MH2538
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid