p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 23rd, 2008, 05:39 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default About treeview data display

what kind of procedure is to maintain to display data as treeview & how will be the design of database?
plz,can any one give me an example?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old December 23rd, 2008, 11:51 AM
Imar's Avatar
Wrox Author
Points: 30,584, Level: 76
Points: 30,584, Level: 76 Points: 30,584, Level: 76 Points: 30,584, Level: 76
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 9,716
Thanks: 5
Thanked 96 Times in 95 Posts
Default

Hi there,

One way is to store tree data in a tabe with a self join. E.g.:

Code:
 
Id        Name            ParentId
1         Root            <null>
2         Child 1         1
3         Child 2         1
4         Grand Child 1   3

The ParentId column then refers to the Id column of the same table.

This way, you can always see to what parent an item belongs.

You can learn more about trees in ASP here:

http://www.codeproject.com/KB/asp/treecontrol.aspx
http://www.planet-source-code.com/vb...txtCodeId=6411

Hope this helps,

Imar
__________________
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

Last edited by Imar : December 23rd, 2008 at 11:54 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 23rd, 2008, 05:55 PM
Friend of Wrox
Points: 4,259, Level: 27
Points: 4,259, Level: 27 Points: 4,259, Level: 27 Points: 4,259, Level: 27
Activity: 63%
Activity: 63% Activity: 63% Activity: 63%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,197
Thanks: 3
Thanked 57 Times in 56 Posts
Default

The problem with storing data such as Imar shows is that retrieving the data in tree-order via a SQL query is nearly impossible if the "depth" of the tree is unknown and/or large.

You have to JOIN the table to itself as many times are there are levels in the tree. One way to do this is via a UNION:
Code:
SELECT id AS level1, 0 AS level2, 0 AS level3, name
FROM table WHERE parentID = 0
UNION
SELECT T1.id AS level1, T2.id AS level2, 0 AS level3, T2.name
FROM table AS T1, table AS T2
WHERE T1.parentID = 0 AND T2.parentID = T1.id
UNION
SELECT T1.id AS level1, T2.id AS level2, T3.id AS level3, T3.name
FROM table AS T1, table AS T2, table AS T3
WHERE T1.parentID = 0 AND T2.parentID = T1.id AND T3.parentID = T2.id
ORDER BY level1, level2, level3
And that works. But what about if there might be 17 levels in the tree?

Another way is to better organize the DB.

Something like this:
Code:
Id        Name            ParentId   Path
1         Root            0          0001-
2         Child 1         1          0001-0002-
3         Child 2         1          0001-0003-
4         GC 1 of C2      3          0001-0003-0004-
5         GC 1 of C1      2          0001-0002-0005-
6         Child 3         1          0001-0006-
7         GC 2 of C1      2          0001-0002-0007-
8         GC 1 Of C3      6          0001-0006-0008-
9         GGC1/GC1/C1     5          0001-0002-0005-0009
And now, if you simply do
Code:
SELECT * FROM table ORDER BY path
it all comes out in ready-to-use-in-your-tree order, thus:
Code:
Id        Name            ParentId   Path
1         Root            0          0001-
2         Child 1         1          0001-0002-
5         GC 1 of C1      2          0001-0002-0005-
9         GGC1/GC1/C1     5          0001-0002-0005-0009
7         GC 2 of C1      2          0001-0002-0007-
3         Child 2         1          0001-0003-
4         GC 1 of C2      3          0001-0003-0004-
6         Child 3         1          0001-0006-
8         GC 1 Of C3      6          0001-0006-0008-
It's a bit more work to put the PATH value into the records as you build the table (you need to convert the id's to strings and pad them out to all the same length, as I show there) but it's well worth the trouble for large trees or trees with a lot of depth.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old December 24th, 2008, 12:22 PM
Imar's Avatar
Wrox Author
Points: 30,584, Level: 76
Points: 30,584, Level: 76 Points: 30,584, Level: 76 Points: 30,584, Level: 76
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 9,716
Thanks: 5
Thanked 96 Times in 95 Posts
Default

With the new Common Table Expressions in SQL Server 2005 and later, it's a lot easier to get complex trees from SQL Server.

But I agree that your solution makes it a lot easier to get your trees available in code.

Cheers,

Imar
__________________
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old December 24th, 2008, 09:36 PM
Friend of Wrox
Points: 4,259, Level: 27
Points: 4,259, Level: 27 Points: 4,259, Level: 27 Points: 4,259, Level: 27
Activity: 63%
Activity: 63% Activity: 63% Activity: 63%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,197
Thanks: 3
Thanked 57 Times in 56 Posts
Default

Yes, I keep forgetting about the availability of recursion in SQL Server 2005 and later. So far as I know, that's the only DB that supports such. Do you know of any other?

FWIW, the system I show works in Access and MySQL and Oracle, and of course SQL Server 2000. Even with Access, with a bit of cleverness in building the PATH value (not as simple as I thow there, that is), you can go 63 levels deep in a table with up to 16 million records. [Use MOD 64 encoding with no delimiters, so 4 bytes per level, so even 255 character limit in Access gets 63 levels. Easy to change to Unicode and get many more than that.]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old December 25th, 2008, 05:35 AM
Imar's Avatar
Wrox Author
Points: 30,584, Level: 76
Points: 30,584, Level: 76 Points: 30,584, Level: 76 Points: 30,584, Level: 76
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 9,716
Thanks: 5
Thanked 96 Times in 95 Posts
Default

I don't know of other DBMS's that can do this, but that doesn't necessarily mean they don't exist... ;-)

Imar
__________________
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CMS: Bind Nav Data to Treeview/Menu controls bullwinkle BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 October 25th, 2007 02:33 PM
Display data within data range? ktsis ASP.NET 1.0 and 1.1 Basics 2 June 9th, 2006 03:06 PM
Display data in a subform marcin2k Access VBA 1 March 1st, 2005 03:37 PM
display report twice with same data pallavijyo BOOK: Professional Crystal Reports for VS.NET 0 December 17th, 2004 04:22 PM
Display Data. mistry_bhavin ADO.NET 2 May 4th, 2004 09:22 AM



All times are GMT -4. The time now is 09:40 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc