Hi all,
I am trying to populate tree view from the database. Till two levels i can populate it fine but when it reaches third level it doesn't expand. Below is the code, it has been taken from the reference of micrososft
Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Call the appropriate method to populate a node at a particular level.
If e.Node.ChildNodes.Count = 0 Then
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateCategories(e.Node)
Case 1
' Populate the second-level nodes.
PopulateProducts(e.Node)
Case 2
' Populate the third-level nodes.
PopulateResources(e.Node)
Case Else
'do mothing
End Select
Else
e.Node.Parent.Text = "xx"
End If
End Sub
Sub PopulateCategories(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("SELECT Block4.BlockCode, sessiontype.type, sessiontype.BlockID,sessiontype.SessionID FROM Block4 INNER JOIN sessiontype ON Block4.BlockID = sessiontype.BlockID Where Block4.BlockCode='xx'")
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim newNode As TreeNode = New TreeNode()
'newNode.Text = "Seminars"
newNode.Text = row("type").ToString()
newNode.Value = row("SessionID").ToString()
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
newNode.PopulateOnDemand = True
' Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode)
Next
End If
End Sub
Sub PopulateProducts(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("SELECT semsymp4.stitle,semsymp4.ID,sessiontype.type FROM semsymp4 INNER JOIN sessiontype ON semsymp4.SessionID = sessiontype.SessionID where semsymp4.SessionID=" & node.Value)
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim newNode As TreeNode = New TreeNode()
'newNode.Text = "Seminars"
newNode.Text = row("stitle").ToString()
newNode.Value = row("ID").ToString()
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
newNode.PopulateOnDemand = True
' Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode)
Next
End If
End Sub
Sub PopulateResources(ByVal node As TreeNode)
' Query for the products of the current category. These are the values
' for the third-level nodes.
Dim ResultSet As DataSet = RunQuery("SELECT Resources.ResourceID, Resources.ResourceTitle,Resources.ResourceURL,Reso urces.ID, semsymp4.ID AS Expr1, semsymp4.stitle FROM Resources INNER JOIN semsymp4 ON Resources.ID = semsymp4.ID where Resources.ID=" & node.Value)
' Create the third-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node.
Dim NewNode As TreeNode = New TreeNode(row("ResourceTitle").ToString())
'NewNode.NavigateUrl = row("ResourceURL").ToString
' Set the PopulateOnDemand property to false, because these are leaf nodes and
' do not need to be populated.
NewNode.PopulateOnDemand = False
' Set additional properties for the node.
'NewNode.SelectAction = TreeNodeSelectAction.None
'
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
Next
End If
End Sub
Function RunQuery(ByVal QueryString As String) As DataSet
' Declare the connection string. This example uses Microsoft SQL Server
' and connects to the Northwind sample database.
Dim ConnectionString As String = "xxx"
Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
Dim DBAdapter As SqlDataAdapter
Dim ResultsDataSet As DataSet = New DataSet
Response.Write(QueryString)
' Try
' Run the query and create a DataSet.
DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
DBAdapter.Fill(ResultsDataSet)
' Close the database connection.
DBConnection.Close()
'Catch ex As Exception
' Close the database connection if it is still open.
If DBConnection.State = ConnectionState.Open Then
DBConnection.Close()
End If
'Message.Text = "Unable to connect to the database."
' End Try
Return ResultsDataSet
End Function
I don't understand what the problem is ,
Please, if you could help me. It's urgent.