Hi,
apologies at first as my vb.net experience is only a few days old...
I am trying to populate a treeview via data stored in an sql2005 db.
The stored proc returns the data as follows:
Asian_Elephants Fishing_Cat Pump
Asian_Elephants Otters Pump
GSO Leopard_Seal Pump
GSO Penguin Pump
Each column represents a sub level of the tree.
ie Pump is a sub level of Fishing_Cat.
Fishing_Cat and Otters are a sub levels of Asian_Elephants.
How do i loop through and build the tree?
Should i be using a Datatable of datareader?
I have tried with the datareader but i think it may be the wrong method.
My code is below. it bombs out after i try to map the newarea after the reader.NextResult() statement saying there is no data.
The code needs to be tweaked to add the nodes at the correct level but that is not my concern at the moment - i just need to get the result set loop happening.
Any help is truly appreciated.
Dim myconnection As New SqlConnection("Data Source=ICAUTOLTP99\SQL2005;Initial Catalog=TarongaZoo;Integrated Security=True")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
Dim newprecinct As String
Dim oldprecinct As String
Dim newarea As String
Dim oldarea As String
Dim newobjecttype As String
Dim oldobjecttype As String
cmd.CommandText = "app_display_menu"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = myconnection
myconnection.Open()
reader = cmd.ExecuteReader
'reader mappings (0)=PRECINCT, (1)=AREA, (2)=TYPE
Do While reader.HasRows
Do While reader.Read
''map parent level
newprecinct = (reader.GetString(0))
oldprecinct = (reader.GetString(0))
'map child1 level
newarea = (reader.GetString(1))
oldarea = (reader.GetString(1))
'map child2 level
newobjecttype = (reader.GetString(2))
oldobjecttype = (reader.GetString(2))
'precinct level
TreeView1.Nodes.Add(newprecinct)
'child1 level
Do Until newprecinct <> oldprecinct
oldprecinct = newprecinct
'insert child1
TreeView1.Nodes.Add(newarea)
'child2 level
Do Until newarea <> oldarea
oldarea = newarea
'insert child2 level
TreeView1.Nodes.Add(newobjecttype)
reader.NextResult()
newarea = (reader.GetString(1))
Loop
Loop
Loop
Loop
myconnection.Close()
apologies at first as my vb.net experience is only a few days old...
I am trying to populate a treeview via data stored in an sql2005 db.
The stored proc returns the data as follows:
Asian_Elephants Fishing_Cat Pump
Asian_Elephants Otters Pump
GSO Leopard_Seal Pump
GSO Penguin Pump
Each column represents a sub level of the tree.
ie Pump is a sub level of Fishing_Cat.
Fishing_Cat and Otters are a sub levels of Asian_Elephants.
How do i loop through and build the tree?
Should i be using a Datatable of datareader?
I have tried with the datareader but i think it may be the wrong method.
My code is below. it bombs out after i try to map the newarea after the reader.NextResult() statement saying there is no data.
The code needs to be tweaked to add the nodes at the correct level but that is not my concern at the moment - i just need to get the result set loop happening.
Any help is truly appreciated.
Dim myconnection As New SqlConnection("Data Source=ICAUTOLTP99\SQL2005;Initial Catalog=TarongaZoo;Integrated Security=True")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
Dim newprecinct As String
Dim oldprecinct As String
Dim newarea As String
Dim oldarea As String
Dim newobjecttype As String
Dim oldobjecttype As String
cmd.CommandText = "app_display_menu"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = myconnection
myconnection.Open()
reader = cmd.ExecuteReader
'reader mappings (0)=PRECINCT, (1)=AREA, (2)=TYPE
Do While reader.HasRows
Do While reader.Read
''map parent level
newprecinct = (reader.GetString(0))
oldprecinct = (reader.GetString(0))
'map child1 level
newarea = (reader.GetString(1))
oldarea = (reader.GetString(1))
'map child2 level
newobjecttype = (reader.GetString(2))
oldobjecttype = (reader.GetString(2))
'precinct level
TreeView1.Nodes.Add(newprecinct)
'child1 level
Do Until newprecinct <> oldprecinct
oldprecinct = newprecinct
'insert child1
TreeView1.Nodes.Add(newarea)
'child2 level
Do Until newarea <> oldarea
oldarea = newarea
'insert child2 level
TreeView1.Nodes.Add(newobjecttype)
reader.NextResult()
newarea = (reader.GetString(1))
Loop
Loop
Loop
Loop
myconnection.Close()