Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Vb 2008 express edition and am very new to treeviews. i have basic knowledge of how to connect to a database. the database i am working with is a microsoft access database and has a large amount of tables with various information. two of these tables i need to put into a treeview. one has 2 columns called date and date id, the date will be the main nodes on the treeview. the other table has 8 colums, among them are the corresponding date id's from the first table, the purchase order id and the purchase order number. the child nodes will be the purchase order number.

Now i know there are a bunch of tutorials out there on treeview population through microsoft access databases but i have found none specifically with what i need, they are all just about dumping ALL the data from the database into the table. i just want specific contents of two tables. if someone could help me out with this i would be very grateful. i can give more information if needed on what i am working with or anything else.!

here is my code as of now, there are two variants i have been trying both are included

VB
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1
    Dim mypath = "I:\Casegoods\Database\Expertpetrolia.mdb"
    Dim conn As New OleDbConnection("Provider = microsoft.ace.oledb.12.0;Data Source=" & mypath)
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim sql1 = "SELECT * FROM Supply_Date ORDER BY [Due_Date]"
    Dim sql2 = "SELECT * FROM Supply_Date INNER JOIN Total_Purchase ON Supply_Date.Date_Id = Total_Purchase.Date_Id ORDER BY [Due_Date]"

    Dim dr2 As OleDbDataReader

    'Dim sql2 = "SELECT * FROM Total_Purchase ORDER BY [Date_Id]"
    Dim cmd2 As OleDbCommand






    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim cmd As OleDbCommand
        Using conn



            cmd = New OleDbCommand(sql1, conn)
            cmd2 = New OleDbCommand(sql2, conn)
            conn.Open()

            dr = cmd.ExecuteReader
            dr2 = cmd2.ExecuteReader

            If dr.HasRows Then
                Do While dr.Read()
                    Dim parent As String = (dr.Item("Due_Date"))

                    If DateDiff(Microsoft.VisualBasic.DateInterval.Day, Today, (dr.Item("Due_Date"))) > -10 Then
                        TreeView1.Nodes.Add(parent)
                        If (dr2.Item("Date_Id")) = (dr.Item("Date_Id")) Then
                            TreeView1.Nodes(parent).Nodes.Add(dr2.Item("Purchase_Order_Number"))

                        End If

                    End If


                Loop
            Else
                MsgBox("No Things found")
            End If



            dr.Close()

        End Using

    End Sub
    'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    '    Dim cmd As OleDbCommand
    '    Using conn



    '        cmd = New OleDbCommand(sql1, conn)
    '        'cmd2 = New OleDbCommand(sql2, conn)
    '        conn.Open()


    '        Dim dr As OleDbDataReader = cmd.ExecuteReader
    '        ' Dim dr2 As OleDbDataReader = cmd2.ExecuteReader

    '        Do While dr.Read()
    '            Dim str1 As String = dr.Item("Due_Date")
    '            Dim str2 As String = dr.Item("Date_Id")
    '            Try
    '                Dim str3 As String = dr.Item("date_id")
    '                Dim str4 As String = dr.Item("Purchase_Order_Number")
    '                If DateDiff(Microsoft.VisualBasic.DateInterval.Day, Today, (dr.Item("Due_Date"))) > -10 Then
    '                    TreeView1.Nodes.Add(str1)
    '                    If str2 = str3 Then
    '                        TreeView1.Nodes(0).Nodes.Add(New TreeNode(str4))
    '                    End If
    '                End If
    '            Catch ex As Exception
    '                MsgBox(ex.Message, MsgBoxStyle.Critical, "stupid dims")
    '            End Try


    '        Loop

    '    End Using

    'End Sub
End Class
Posted
Updated 14-May-14 3:58am
v3
Comments
wikus70 14-May-14 1:56am    
I'm not sure what your question is. Are you having trouble selecting the proper info from the database or are you having trouble populating the nodes/sub-nodes of the treeview?
NickHallick 14-May-14 7:37am    
I am having difficulty with both. referencing the two different table values and displaying them in a tree view.

Have a look here: Loading a TreeView using HierarchyID[^].
There is a sample algorithm implementation for sql server database, but the main idea is the same. You need to change it to your needs.
 
Share this answer
 
You'll have to play around a bit with this code to get it working for you.
Database connection and data selection:
VB
Dim Connection As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDatabseName.mdb;")
Dim Command As New Data.OleDb.OleDbCommand("SELECT * FROM TableName", Connection)

Connection.Open()
Dim Reader As OleDbDataReader = Command.ExecuteReader

Do While Reader.Read()
    Dim str1 As String = Reader.Item("Column1Name")
    Dim str2 As String = Reader.Item("Column2Name")
    Dim str3 As String = Reader.Item("Column3Name")
Loop

Connection.Close()


As for updating the treeview:
VB
TreeView1.Nodes.Add(New TreeNode("NewNode"))
TreeView1.Nodes(0).Nodes.Add(New TreeNode("ChildNode"))
TreeView1.Nodes(0).Nodes.Add(New TreeNode("ChildNode"))
TreeView1.Nodes(0).Nodes(0).Nodes.Add(New TreeNode("ChildChildNode"))
 
Share this answer
 
Comments
NickHallick 14-May-14 8:35am    
so what are the str1,str2,str3 for? are those put into the second part for updating the treeview?
wikus70 14-May-14 8:43am    
Yes. The "Do While" will loop through the table one row at a time. The Reader.Item is used to get the data. That data will be used to create a new node in the treeview. Creating the nodes will also have to be in the "Do While" loop though.

So you'll be replaying "NewNode", "ChildNode" and "ChildChildNode" with str1, str2 and str3
NickHallick 14-May-14 9:04am    
okay the only thing im having problems with is getting data from the second table. i can get the data i need from the first table and make parent nodes from it but when i try to read a column from the second table it gives me an error "no data exists for this row/column"
wikus70 14-May-14 9:56am    
Can you edit your question to include you code?
NickHallick 14-May-14 9:59am    
ive added my entire code, i have a couple variants that i have been trying

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900