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(By Val sender As Object, ByVal e As TreeNodeEventAr gs)
' Call the appropriate method to populate a node at a particular level.
If e.Node.ChildNod es.Count = 0 Then
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateCategor ies(e.Node)
Case 1
' Populate the second-level nodes.
PopulateProduct s(e.Node)
Case 2
' Populate the third-level nodes.
PopulateResourc es(e.Node)
Case Else
'do mothing
End Select
Else
e.Node.Parent.T ext = "xx"
End If
End Sub
Sub PopulateCategor ies(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("SELEC T Block4.BlockCod e, sessiontype.typ e, sessiontype.Blo ckID,sessiontyp e.SessionID FROM Block4 INNER JOIN sessiontype ON Block4.BlockID = sessiontype.Blo ckID Where Block4.BlockCod e='xx'")
' Create the second-level nodes.
If ResultSet.Table s.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.Table s(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").ToS tring()
newNode.Value = row("SessionID" ).ToString()
' Set the PopulateOnDeman d property to true so that the child nodes can be
' dynamically populated.
newNode.Populat eOnDemand = True
' Set additional properties for the node.
newNode.SelectA ction = TreeNodeSelectA ction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes .Add(newNode)
Next
End If
End Sub
Sub PopulateProduct s(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("SELEC T semsymp4.stitle ,semsymp4.ID,se ssiontype.type FROM semsymp4 INNER JOIN sessiontype ON semsymp4.Sessio nID = sessiontype.Ses sionID where semsymp4.Sessio nID=" & node.Value)
' Create the second-level nodes.
If ResultSet.Table s.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.Table s(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").T oString()
newNode.Value = row("ID").ToStr ing()
' Set the PopulateOnDeman d property to true so that the child nodes can be
' dynamically populated.
newNode.Populat eOnDemand = True
' Set additional properties for the node.
newNode.SelectA ction = TreeNodeSelectA ction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes .Add(newNode)
Next
End If
End Sub
Sub PopulateResourc es(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("SELEC T Resources.Resou rceID, Resources.Resou rceTitle,Resour ces.ResourceURL ,Resources.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.Table s.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.Table s(0).Rows
' Create the new node.
Dim NewNode As TreeNode = New TreeNode(row("R esourceTitle"). ToString())
'NewNode.Naviga teUrl = row("ResourceUR L").ToString
' Set the PopulateOnDeman d property to false, because these are leaf nodes and
' do not need to be populated.
NewNode.Populat eOnDemand = False
' Set additional properties for the node.
'NewNode.Select Action = TreeNodeSelectA ction.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 ConnectionStrin g As String = "xxx"
Dim DBConnection As SqlConnection = New SqlConnection(C onnectionString )
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.Cl ose()
'Catch ex As Exception
' Close the database connection if it is still open.
If DBConnection.St ate = ConnectionState .Open Then
DBConnection.Cl ose()
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.