Hi,
I am trying to learn how to populate a tree, I did this same thing back in Vb6, but I am having trouble with the data reader here. I do not think I have it set up right. It gets to the line with
Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
and just jumps put leaving the tree blank; and it gives no error message.
Can someone please tell me what I am doing wrong.
Thank you
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()
I am trying to learn how to populate a tree, I did this same thing back in Vb6, but I am having trouble with the data reader here. I do not think I have it set up right. It gets to the line with
Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
and just jumps put leaving the tree blank; and it gives no error message.
Can someone please tell me what I am doing wrong.
Thank you
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()