473,657 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.Ole Db.OleDbConnect ion
Dim MyCommand As System.Data.Ole Db.OleDbDataAda pter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbData Adapter
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.Ole Db.OleDbConnect ion( _
"provider=Micro soft.Jet.OLEDB. 4.0; " & _
"data source=C:\Inetp ub\wwwroot\inte lis.MDB")
MyCommand = New System.Data.Ole Db.OleDbDataAda pter( _
"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("ENTI TY").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.Cl ose()
MyConnection.Di spose()
End If

End Sub

--
Hutty
Nov 19 '05 #1
5 1818
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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostback then
Dim strConnectionSt ring As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lnam e,
authors.au_fnam e from authors order by authors.au_lnam e, authors.au_fnam e,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_ id, titles.title
from titleauthor Inner Join titles on titleauthor.tit le_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(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()

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

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

ds.Relations.Ad d("AuthorTitle" , _
ds.Tables("Auth ors").Columns(" au_id"), _
ds.Tables("Titl es").Columns("a u_id"))

For Each rowAuthor in ds.Tables("Auth ors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_l name") & "," _
& rowauthor("au_f name")
TreeView1.Nodes .Add(nodeAuthor )
Dim nodeTitle as TreeNode

For Each rowTitle In rowauthor.GetCh ildRows("Author Title")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next
Next

ds.Dispose()
cmauthors.Dispo se
cmTitles.Dispos e
cnn.Close
cnn.Dispose
End If

End Sub

Private Sub TreeView1_Expan d(ByVal sender As Object, ByVal e As
Microsoft.Web.U I.WebControls.T reeViewClickEve ntArgs) Handles TreeView1.Expan d
Dim strConnectionSt ring 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.Node s.Count = 0 Then
cnn = New SqlConnection(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id =titleauthor.ti tle_id where titleauthor.au_ id =
nodeAuthor.Node Data.ToString Order by titles.title"
cmTitles = New SqlDataAdapter( strSQLTitles, cnn)
cmTitles.Fill(d s, "Titles")

For Each rowTitle in ds.Tables("Titl es").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next

ds.Dispose()
cmTitles.Dispos e
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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.Ole Db.OleDbConnect ion
Dim MyCommand As System.Data.Ole Db.OleDbDataAda pter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbData Adapter
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.Ole Db.OleDbConnect ion( _
"provider=Micro soft.Jet.OLEDB. 4.0; " & _
"data source=C:\Inetp ub\wwwroot\inte lis.MDB")
MyCommand = New System.Data.Ole Db.OleDbDataAda pter( _
"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("ENTI TY").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.Cl ose()
MyConnection.Di spose()
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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostback then
Dim strConnectionSt ring As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lnam e,
authors.au_fnam e from authors order by authors.au_lnam e, authors.au_fnam e,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_ id, titles.title
from titleauthor Inner Join titles on titleauthor.tit le_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(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()

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

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

ds.Relations.Ad d("AuthorTitle" , _
ds.Tables("Auth ors").Columns(" au_id"), _
ds.Tables("Titl es").Columns("a u_id"))

For Each rowAuthor in ds.Tables("Auth ors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_l name") & "," _
& rowauthor("au_f name")
TreeView1.Nodes .Add(nodeAuthor )
Dim nodeTitle as TreeNode

For Each rowTitle In rowauthor.GetCh ildRows("Author Title")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next
Next

ds.Dispose()
cmauthors.Dispo se
cmTitles.Dispos e
cnn.Close
cnn.Dispose
End If

End Sub

Private Sub TreeView1_Expan d(ByVal sender As Object, ByVal e As
Microsoft.Web.U I.WebControls.T reeViewClickEve ntArgs) Handles TreeView1.Expan d
Dim strConnectionSt ring 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.Node s.Count = 0 Then
cnn = New SqlConnection(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id =titleauthor.ti tle_id where titleauthor.au_ id =
nodeAuthor.Node Data.ToString Order by titles.title"
cmTitles = New SqlDataAdapter( strSQLTitles, cnn)
cmTitles.Fill(d s, "Titles")

For Each rowTitle in ds.Tables("Titl es").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next

ds.Dispose()
cmTitles.Dispos e
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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.Ole Db.OleDbConnect ion
Dim MyCommand As System.Data.Ole Db.OleDbDataAda pter

Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbData Adapter
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.Ole Db.OleDbConnect ion( _
"provider=Micro soft.Jet.OLEDB. 4.0; " & _
"data source=C:\Inetp ub\wwwroot\inte lis.MDB")
MyCommand = New System.Data.Ole Db.OleDbDataAda pter( _
"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("ENTI TY").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.Cl ose()
MyConnection.Di spose()
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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then

Dim MyConnection As System.Data.Ole Db.OleDbConnect ion
Dim MyCommand As System.Data.Ole Db.OleDbDataAda pter
Dim MyCommand2 As System.Data.Ole Db.OleDbDataAda pter

Dim dt As DataTable
Dim Tn As TreeNode
Dim Tn2 As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbData Adapter
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.Ole Db.OleDbConnect ion( _
"provider=Micro soft.Jet.OLEDB. 4.0; " & _
"data source=C:\Inetp ub\wwwroot\inte lis.MDB")
MyCommand = New System.Data.Ole Db.OleDbDataAda pter( _
"select DISTINCT PARENT, ID from Ent1",
MyConnection)
MyCommand2 = New System.Data.Ole Db.OleDbDataAda pter( _
"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.Ad d("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(Tn 2)
Next

Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Cl ose()
MyConnection.Di spose()
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
11520
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
3232
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 Semester Code Task --------------------------------------------------- "2003" "One" "CSE9020" "Deliverable Item 1" "2003" "One" "CSE9020" "Deliverable Item 2"
6
1518
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 ids. I have built a collection of the right number of ids. I am using the following code to recursively go through the tree (some code left out). Pos is the position in the collection of ids - I need to select the correct ids (need to use previous...
3
10140
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 that. Im trying for about 3 hours but can't get it to work. Thank you!
7
2827
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 of custom, extended nodes, which include all the extra information contained in the table and not just typical TreeNode objects. To do that, I first created a structure with all the extra
1
18667
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 them from the Table back into the TreeView when the app starts up. (If the table is empty, then just exit the sub)
5
5807
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, the options they are allowed to see are all different and specified in a MSACCESS table. Can I populate a Treeview directly from my MSAccess table ? I can then of course filter the table to only show that users options.
2
2592
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 document from a SQL Query. The problem is that the Treeview renders correctly and expands with the appropriate hierarchy but clicking anything but the very top node gives me a NULL Reference Exception. I haven't even got to the stage of doing anything...
2
5503
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 programmatically. And I want to bind the selection of a TreeNode (which is a record) from the database) to a DetailsView which shows the datatable record for the selected TreeView node in the Details View. I am using IDE : Visual Web Developer 2005 Express; OS=...
0
1896
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 current selection and stuff in the tree. I post my code here, any idea ?
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.