4 Replies - 6256 Views - Last Post: 18 May 2010 - 07:07 PM Rate Topic: -----

#1 itmasterw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 10

trying to populate a treeview

Posted 17 May 2010 - 04:36 PM

I am trying to learn how to populate a tree from a database, I did this same thing back in Vb6 and am trying to learnhow to do it in VB.Net, but I am having trouble with the code. From looking on the web I have piced this together, but I think it may be far off. It just jumps out put leaving the tree blank; and it gives no error message. I put in a try error catch and it cannot convert the data to a double, but even if I take this out it does not work. It is stuck at the line with :
dr("CustomerID") <> hID
I was using this to check for a parent node, I can take it out, but it still does not work; I am afraid that I am be way off here. I am using the Nothwind database.
Can someone please tell me what I am doing wrong.
Thank you

Here is my code:

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

Dim connString As String = Util.SQLbConnect
Dim conn As New SqlClient.SqlConnection(connString)
Dim cmdString As String = "SELECT Distinct Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID Where Orders.CustomerID = '" + txtCustID.Text + "')"
Dim cmd As New SqlClient.SqlCommand(cmdString, conn)
conn.Open()

Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()

Dim hID As Integer = 0
Dim parentnode As TreeNode = Nothing

While dr.Read
If dr("CustomerID") <> hID Then
hID = dr("CustomerID")
parentnode = New TreeNode(dr("CompanyName"))
If dr("Country") = "Canada" Then
parentnode.ForeColor = Color.Red
Else
parentnode.ForeColor = Color.Black
End If
TreeView1.Nodes.Add(parentnode)
End If
Dim childnode As New TreeNode(dr("SubComapnyName"))
If dr("SubCountry") = "Canada" Then
childnode.ForeColor = Color.Red
Else
childnode.ForeColor = Color.Black
End If
parentnode.Nodes.Add(childnode)
End While
dr.Close()


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: trying to populate a treeview

#2 Guest_Itmasterw*


Reputation:

Re: trying to populate a treeview

Posted 18 May 2010 - 06:13 AM

View Postitmasterw, on 17 May 2010 - 03:36 PM, said:

I am trying to learn how to populate a tree from a database, I did this same thing back in Vb6 and am trying to learnhow to do it in VB.Net, but I am having trouble with the code. From looking on the web I have piced this together, but I think it may be far off. It just jumps out put leaving the tree blank; and it gives no error message. I put in a try error catch and it cannot convert the data to a double, but even if I take this out it does not work. It is stuck at the line with :
dr("CustomerID") <> hID
I was using this to check for a parent node, I can take it out, but it still does not work; I am afraid that I am be way off here. I am using the Nothwind database.
Can someone please tell me what I am doing wrong.
Thank you

Here is my code:

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

Dim connString As String = Util.SQLbConnect
Dim conn As New SqlClient.SqlConnection(connString)
Dim cmdString As String = "SELECT Distinct Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID Where Orders.CustomerID = '" + txtCustID.Text + "')"
Dim cmd As New SqlClient.SqlCommand(cmdString, conn)
conn.Open()

Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()

Dim hID As Integer = 0
Dim parentnode As TreeNode = Nothing

While dr.Read
If dr("CustomerID") <> hID Then
hID = dr("CustomerID")
parentnode = New TreeNode(dr("CompanyName"))
If dr("Country") = "Canada" Then
parentnode.ForeColor = Color.Red
Else
parentnode.ForeColor = Color.Black
End If
TreeView1.Nodes.Add(parentnode)
End If
Dim childnode As New TreeNode(dr("SubComapnyName"))
If dr("SubCountry") = "Canada" Then
childnode.ForeColor = Color.Red
Else
childnode.ForeColor = Color.Black
End If
parentnode.Nodes.Add(childnode)
End While
dr.Close()


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Was This Post Helpful? 0

#3 Guest_Itmasterw*


Reputation:

Re: trying to populate a treeview

Posted 18 May 2010 - 06:17 AM

Unless something happened I did use code tags and now I would not know how tongi back in and corect it.
Was This Post Helpful? 0

#4 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: trying to populate a treeview

Posted 18 May 2010 - 11:54 AM

OK, this isn't exactly what you are doing but I had it handy and it should point you in the right direction.

      Dim cnnString As String = "YOUR CONNECTION STRING"
        Dim sql As String = "Select lastname + ', ' + firstname as Name, lastname from Employees order by Lastname"
        Dim rdr As SqlDataReader
        Dim cnn As SqlConnection = New SqlConnection(cnnString)
        Dim cmd As SqlCommand = New SqlCommand(sql, cnn)

        'open connection
        Try
            cnn.Open()
            rdr = cmd.ExecuteReader()

            'Add Alphabet letter to each node of Treeview
            Dim I As Integer
            Dim node_letter As String
            'Set the Document Type
            tv1.Nodes.Add("Contacts")

            'Set the Documents
            tv1.SelectedNode = tv1.Nodes.Item(0)

            tv1.ExpandAll()
            tv1.ShowLines = True
            tv1.ShowPlusMinus = True
            For I = 1 To 26
                node_letter = Chr(64 + I)
                tv1.SelectedNode.Nodes.Add(node_letter)
            Next I

            '  Add database records to appropriate Alphabet Node
            Dim node_key As String
            Dim entry_letter As String
            Dim node_name As String

            Do While rdr.Read
                node_key = rdr.GetString(0)
                entry_letter = node_key.Substring(0, 1)
                Dim myNode As TreeNode
                I = 0
                node_name = rdr.GetString(0)
                For Each myNode In tv1.Nodes.Item(0).Nodes
                    If myNode.Text = entry_letter Then
                        tv1.Nodes.Item(0).Nodes.Item(myNode.Index).Nodes.Add(node_name)
                        Exit For
                    End If
                Next
            Loop

        Catch ex As Exception
            MsgBox("Failed to load" & vbCrLf & ex.Message)
        Finally
            cnn.Close()
        End Try

        tv1.Nodes(0).Expand()


Was This Post Helpful? 0
  • +
  • -

#5 itmasterw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 10

Re: trying to populate a treeview

Posted 18 May 2010 - 07:07 PM

Thanks I really appreciate your help. I have been looking all over and could not get anything to work. I am sure that this will help me learn how to do this for my project.
Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1