I am trying to learn how to populate a tree, 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. It just jumps out put leaving the tree blank; and it gives no error message. 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 y 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()

Edited 6 Years Ago by __avd: Added [code] tags.

Comments
Do not flood the forum by posting the same question more than once (ie in multiple forums).

Yes I was looking for them but could not find what I would use here. If you can tell me I woudl be happy to use them.
Thank you

I am trying to learn how to populate a tree, 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. It just jumps out put leaving the tree blank; and it gives no error message. 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 y 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()

end quote.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I can not figure out how to edit this to put in the code tags so I am going to repost this is that okay?

Please use "Flag Bad Post" option to notify moderators that post is in wrong section/missing code-tags and assigned moderator will correct it.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If dr("CustomerID") <> hID Then

you need to do

If dr(1) <> hID Then

Please mention error.

This article has been dead for over six months. Start a new discussion instead.