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()