473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Save and Load a TreeView to SQL Database

Hi all, I am struggling with the following:

Environement:
VB.NET 2005
TreeView Control
SQL Database

I need to be able to save a TreeView's nodes into a SQL database and
then be able to call them from the Table back into the TreeView when
the app starts up. (If the table is empty, then just exit the sub)

The structure of the TreeView will be something like this.

ParentNode
--childNode1
--childNode2
--childNode3
ParentNode2
--childNode1
--childNode2
ParentNode3
--childNode1

The Treeview will only have one level of children nodes for each parent
node.

So my question is, how should I format my table to save this data and
are they any code samples to show me how to do this?

Any assistance would be greatly appreciated.

Mar 1 '06 #1
1 18580
Hi
Suppose you have a table named MyTable with the following columns:
ID
Title
ParentID

add two button and a treeview , store id in treeNode's tag . ( it is
better to use inheritance for this kind of storing data):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SaveTree(TreeView1.Nodes)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim table As DataTable
' populate table with data
LoadTree(table)
End Sub
Private Sub SaveTree(ByVal nodes As TreeNodeCollection)
For Each node As TreeNode In nodes
SaveNode(node)
SaveTree(node.Nodes)
Next
End Sub

Private Sub SaveNode(ByVal Node As TreeNode)
AddRowToMyTable(CInt(Node.Tag), Node.Text,
CInt(Node.Parent.Tag))
End Sub

Private Sub LoadTree(ByVal table As DataTable)
For Each row As DataRow In table.Rows
AddNode(row("ID"), row("Title"), row("ParentID"))
Next
End Sub

Private Sub AddNode(ByVal id As Integer, ByVal title As String,
ByVal parentID As Integer)
Dim newNode As New TreeNode
newNode.Text = title
newNode.Tag = id
If parentID = -1 Then
TreeView1.Nodes.Add(newNode)
Else
Dim parentNode As TreeNode = FindParent(parentID)
parentNode.Nodes.Add(newNode)
End If

End Sub
I hope this helps
A.Hadi

Mar 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JJ | last post by:
Hi All, How in ASP.NET, from PopUp window: 1) - save Data in Database; and after 2) a) - Reload the opener window b) -close PopYp window. Note:
5
by: Hutty | last post by:
I have a table that has two columns, Parent and Child. I'm able to populate a IEControls treeview with the Parent column. On the same row as the Parent is the child in the Child colum. How do...
1
by: simon | last post by:
I have time in my program: for example: dim timeTest as datetime timeTest="10:15:10" I save this time to database as decimal value: timeTest.ToOADate - I get the decimal value and I save...
14
by: Mr.D | last post by:
How do I save/load the contents of a Treeview to a file? I have found several good examples written i VB6, but not a single one for VB.NET. Please help. ---- Tim
2
by: Polaris | last post by:
Hi Experts: I'm using ASP.NET 2.0. I'm using a TreeView control with a design-time configured XmlDataSource (an XML file). I use the TreeView control as a "message board" with each node...
2
by: =?Utf-8?B?SnVhbiBEZW50?= | last post by:
Hi, I am using XmlWriter/XmlReader to save.load classes. However, one of the members of one class is of type Type. How can I write and then read that as Xml? -- Thanks in advance, Juan...
0
Ensonix
by: Ensonix | last post by:
I found a post about this, and it had some C# code that wouldn't convert, and when I finally got it converted it didn't work in .NET 1.1. So I made a few changes to the converted VB version and now...
1
ranjana1980
by: ranjana1980 | last post by:
In my project of desktop application using c#.net save image in database during registration of student But it is not mandatory.Without Browse the image i click on insert button it gives error "Empty...
1
by: kiranbabu | last post by:
<html> <head> <style type="text/css"> h2{color:#A02820} </style> </head> <script language=javascript>
3
by: regan2007 | last post by:
How are you going to save image in database using php?Please explain it if you do kno
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.