Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying too populate a treeview

Status
Not open for further replies.

jakeir

Programmer
Apr 7, 2009
25
US
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()
 
One thing to note, change
Code:
'" + txtCustID.Text + "')"

to
Code:
'" [RED]&[/RED] txtCustID.Text [RED]&[/RED] "')"

Also, if you set a breakpoint and test in the intermediate window, are you actually getting values returned?

If at first you don't succeed, then sky diving wasn't meant for you!
 

plus, you do not have any left ( in your SQL, so you do not need the ) in
Code:
'" & txtCustID.Text & "'[red])[/red]"

Have fun.

---- Andy
 
Hi,
And thanks for our help,I definatly need to make these, but it still is returning nothing.
Is there anything else I can do?
 
You will have to use a breakpoint to see what values are being returned.

Since you indicated 'returning nothing', I would recommend starting at conn.Open() to verify if you are in fact making a connection to the database.

Also, have you run the query in SQL to ensure values are actually being retrieved?

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top