473,406 Members | 2,707 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,406 software developers and data experts.

treeview database

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 add the subnodes from the Child
column? Here's what I have so far. Thanks in advance.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim DR As DataRow
Dim DR2 As DataRow

'Create the Connection object and the DataSet object,
'and then open the connection.
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Inetpub\wwwroot\intelis.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select DISTINCT Parent, Child from Entity",
MyConnection)

ds = New DataSet
'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENTITY")

'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENTITY").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If

End Sub

--
Hutty
Nov 19 '05 #1
5 1787
Hi Hutty -

I came across this code when I downloaded the Treeview control. It's using
the Pubs DB. It worked for me -

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostback then
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lname,
authors.au_fname from authors order by authors.au_lname, authors.au_fname,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_id, titles.title
from titleauthor Inner Join titles on titleauthor.title_id = titles.title_id
Order by titles.title"
Dim nodeAuthor as TreeNode
Dim noteTitle as TreeNode
Dim cnn as SqlConnection
Dim cmAuthors as SqlDataAdapter
Dim cmTitles as SqlDataAdapter
Dim ds as DataSet
Dim rowAuthor as DataRow
Dim rowTitle as DataRow

cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()

cmAuthors = New SqlDataAdapter(strSQLAuthors, cnn)
cmAuthors.Fill(ds, "Authors")

cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")

ds.Relations.Add("AuthorTitle", _
ds.Tables("Authors").Columns("au_id"), _
ds.Tables("Titles").Columns("au_id"))

For Each rowAuthor in ds.Tables("Authors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_lname") & "," _
& rowauthor("au_fname")
TreeView1.Nodes.Add(nodeAuthor)
Dim nodeTitle as TreeNode

For Each rowTitle In rowauthor.GetChildRows("AuthorTitle")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next
Next

ds.Dispose()
cmauthors.Dispose
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If

End Sub

Private Sub TreeView1_Expand(ByVal sender As Object, ByVal e As
Microsoft.Web.UI.WebControls.TreeViewClickEventArg s) Handles TreeView1.Expand
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLTitles As String
Dim cnn as SqlConnection
Dim cmTitles As SqlDataAdapter
Dim ds As DataSet
Dim rowTitle as DataRow
Dim nodeTitle As TreeNode
Dim nodeAuthor As TreeNode

nodeAuthor = sender.nodes(e.Node.ToString)
If nodeAuthor.Nodes.Count = 0 Then
cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id=titleauthor.title_id where titleauthor.au_id =
nodeAuthor.NodeData.ToString Order by titles.title"
cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")

For Each rowTitle in ds.Tables("Titles").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next

ds.Dispose()
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If
End Sub

"Hutty" wrote:
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 add the subnodes from the Child
column? Here's what I have so far. Thanks in advance.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim DR As DataRow
Dim DR2 As DataRow

'Create the Connection object and the DataSet object,
'and then open the connection.
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Inetpub\wwwroot\intelis.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select DISTINCT Parent, Child from Entity",
MyConnection)

ds = New DataSet
'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENTITY")

'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENTITY").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If

End Sub

--
Hutty

Nov 19 '05 #2
Thanks for the reply Sandy. Actually, that's the same code that I used to
get where I am now. I just need to study the child relations a little
deeper. Thanks.

"Sandy" wrote:
Hi Hutty -

I came across this code when I downloaded the Treeview control. It's using
the Pubs DB. It worked for me -

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostback then
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lname,
authors.au_fname from authors order by authors.au_lname, authors.au_fname,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_id, titles.title
from titleauthor Inner Join titles on titleauthor.title_id = titles.title_id
Order by titles.title"
Dim nodeAuthor as TreeNode
Dim noteTitle as TreeNode
Dim cnn as SqlConnection
Dim cmAuthors as SqlDataAdapter
Dim cmTitles as SqlDataAdapter
Dim ds as DataSet
Dim rowAuthor as DataRow
Dim rowTitle as DataRow

cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()

cmAuthors = New SqlDataAdapter(strSQLAuthors, cnn)
cmAuthors.Fill(ds, "Authors")

cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")

ds.Relations.Add("AuthorTitle", _
ds.Tables("Authors").Columns("au_id"), _
ds.Tables("Titles").Columns("au_id"))

For Each rowAuthor in ds.Tables("Authors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_lname") & "," _
& rowauthor("au_fname")
TreeView1.Nodes.Add(nodeAuthor)
Dim nodeTitle as TreeNode

For Each rowTitle In rowauthor.GetChildRows("AuthorTitle")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next
Next

ds.Dispose()
cmauthors.Dispose
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If

End Sub

Private Sub TreeView1_Expand(ByVal sender As Object, ByVal e As
Microsoft.Web.UI.WebControls.TreeViewClickEventArg s) Handles TreeView1.Expand
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLTitles As String
Dim cnn as SqlConnection
Dim cmTitles As SqlDataAdapter
Dim ds As DataSet
Dim rowTitle as DataRow
Dim nodeTitle As TreeNode
Dim nodeAuthor As TreeNode

nodeAuthor = sender.nodes(e.Node.ToString)
If nodeAuthor.Nodes.Count = 0 Then
cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id=titleauthor.title_id where titleauthor.au_id =
nodeAuthor.NodeData.ToString Order by titles.title"
cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")

For Each rowTitle in ds.Tables("Titles").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next

ds.Dispose()
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If
End Sub

"Hutty" wrote:
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 add the subnodes from the Child
column? Here's what I have so far. Thanks in advance.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim DR As DataRow
Dim DR2 As DataRow

'Create the Connection object and the DataSet object,
'and then open the connection.
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Inetpub\wwwroot\intelis.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select DISTINCT Parent, Child from Entity",
MyConnection)

ds = New DataSet
'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENTITY")

'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENTITY").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If

End Sub

--
Hutty

Nov 19 '05 #3
Yeah that code is for populating the treeview from the DB.
But has any of u came across a way to manage it.
I mean a front end where someone can modify ,delete and add nodes
dynamically.
Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #4


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #5
I had to create a duplicate table to get the child underneath the parent.
Would have preferred using the same table for both. I have a problem with
the display. Instead of displaying the PARENT once and all the CHILD
underneath, the PARENT is being displayed on multiple lines for each CHILD.
I'm trying to figure how to get the code to recognize PARENT already exist
and not to add it again. In addition, when adding CHILD to see if PARENT
exist and add CHILD underneath. Any ideas? Here's my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyCommand2 As System.Data.OleDb.OleDbDataAdapter

Dim dt As DataTable
Dim Tn As TreeNode
Dim Tn2 As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim DR As DataRow
Dim DR2 As DataRow

'Create the Connection object and the DataSet object,
'and then open the connection.
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Inetpub\wwwroot\intelis.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select DISTINCT PARENT, ID from Ent1",
MyConnection)
MyCommand2 = New System.Data.OleDb.OleDbDataAdapter( _
"select * from Ent2",
MyConnection)

ds = New DataSet
' dt = ds.Tables(0)

'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENT1")
MyCommand2.Fill(ds, "ENT2")
ds.Relations.Add("ENTITIES", ds.Tables("ENT1").Columns("ID"),
ds.Tables("ENT2").Columns("ID"))

'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENT1").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
For Each DR2 In DR.GetChildRows("ENTITIES")
Tn2 = New TreeNode
Tn2.Text = DR2("CHILD")
Tn.Nodes.Add(Tn2)
Next

Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If

End Sub
"Patrick Olurotimi Ige" wrote:


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #6

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

Similar topics

42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
3
by: Soul | last post by:
Hi, I am learning C# at the moment. I am trying to develop a simple program that will get data from a MS Access database into a dataSet. The result of dataSet should be something like: Year ...
6
by: Jan Krouwer | last post by:
I have a treeview which is populated from a relational database. In order to copy part of the tree, I need to add to the database the relationship of the part of the tree to be copied but with new...
3
by: Jan Wrage | last post by:
Hi! I would like to implement a treeview in my existing application. It should show my entire Active-Directory structure, i.e. all Groups, Containers and OUs. Could somebody help me with...
7
by: vsiat | last post by:
I am trying to create a treeview out of a database table with the typical structure ID, NAME, PARENTID, TYPE, EXTRA_INFO, where is linked to the . What I want to achieve is create a tree made...
1
by: cjinsocal581 | last post by:
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...
5
by: Paul | last post by:
Hi, I am a self taught VBA programmer, and I'm trying to learn VB2005 Express (The price was right). I like the look of the treeview control, and I'd like to use it as a menu system for my users,...
2
by: Simon Rigby | last post by:
Hi folks, A bizarre problem I am having. I have a treeview which is bound to an XmlDataSource. The XMLDataSource.Data property is set to the result of a function that generates an XML...
2
by: makennedy | last post by:
Hi Experts, Please help, I am a newbie to ASP.NET 2.0 may be I am doing something wrong or there may be a bug somewhere. Basically I have a TreeView Control which I have created...
0
by: Falcula | last post by:
Hello, I have a treeview that i fill from a database, when i update nodename in database the treeview dont update. Its works when iam not useing enableviewstate="true" but then i loosing the...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.