I'm making a snazzy VB.NET GUI as part of my contract for an asset management firm and it incorporates a TreeView control which I am using to graphically display details of locations, with child nodes for buildings and grandchild nodes as floors per building.
The TreeView nodes are dynamically generated from a SQL Server table (which I cannot disclose details of for confidentiality reasons), however, my problem is pretty generic.
I'm using the HasRows property to check this table for data (so far I've saved two rows of data to it) but it is always returning false on first run (i.e. it can't find those two rows...)
Here's my code:
Expand|Select|Wrap|Line Numbers
- Private Sub LoadLocationNodes(ByVal ClientID As Integer)
- Dim dr_Nodes As DataTableReader
- Dim X As TreeNode
- Dim parentNodeFind As TreeNode()
- Dim parentNode As TreeNode
- dr_Nodes = Mth_collectDataSet.tbl_data_locns.CreateDataReader()
- If dr_Nodes.HasRows Then
- 'Clear existing node list
- For Each X In tvw_Locations.Nodes
- tvw_Locations.Nodes.Remove(X)
- Next
- 'Load node list into TreeView object
- While dr_Nodes.Read
- Dim tempNode As TreeNode = New TreeNode
- Select Case dr_Nodes.Item("ParentID").ToString
- Case "", "0", "00000000-0000-0000-0000-000000000000", DBNull.Value.ToString
- tempNode.Name = dr_Nodes.Item("LocationID").ToString
- tempNode.Text = dr_Nodes.Item("LocationName")
- tempNode.Tag = dr_Nodes.Item("ParentID").ToString
- If dr_Nodes.Item("VisioMap") IsNot DBNull.Value Then
- tempNode.Checked = True
- Else
- tempNode.Checked = False
- End If
- tvw_Locations.Nodes.Add(tempNode)
- Case Else
- tempNode.Name = dr_Nodes.Item("LocationID").ToString
- tempNode.Text = dr_Nodes.Item("LocationName")
- tempNode.Tag = dr_Nodes.Item("ParentID").ToString
- parentNodeFind = tvw_Locations.Nodes.Find(dr_Nodes.Item("ParentID").ToString, True)
- For Each parentNode In parentNodeFind
- parentNode.Nodes.Add(tempNode)
- Next
- End Select
- tempNode = Nothing
- End While
- Else
- AddNewLocation("0")
- End If
- End Sub
The AddNewLocation then calls the LoadLocationNodes function which then only returns the one (new) node/record instead of 3 (one new + two already in DB).
I am working in Visual Studio 2005 Pro with SQL Server 05 Express Edition and the two rows that I have added have been added using INSERT queries run in design mode. If anyone has any ideas where I'm going wrong and could steer me in the right direction it would be most appreciated.
And last but not least - anyone know how to send an UPDATE method back to the database (fired when someone edits the node label in the TreeView control)?
Many thanks in advance,
medicineworker