This is a migrated thread and some comments may be shown as answers.

Populating TreeView Nodes from SQL DB

4 Answers 292 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
jimi
Top achievements
Rank 1
jimi asked on 01 Sep 2008, 10:11 PM
Hello!

I have these original codes (from MSDN) for standard VS 2008 TreeView that works for this table diagram with  minor tweaks. What I wish to find out if there's an equivalent code in Telerik TreeView to implement the same for the same set of tables.

Thanks,

Jimi


This is the code set:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)

    ' Call the appropriate method to populate a node at a particular level.
    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 Else
        ' Do nothing.

    End Select

  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 CategoryID, CategoryName From Categories")

    ' 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(row("CategoryName").ToString(), row("CategoryID").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 products of the current category. These are the values
    ' for the third-level nodes.
    Dim ResultSet As DataSet = RunQuery("Select ProductName From Products Where CategoryID=" & 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("ProductName").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 = "server=localhost;database=NorthWind;Integrated Security=SSPI"

    Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
    Dim DBAdapter As SqlDataAdapter
    Dim ResultsDataSet As DataSet = New DataSet

    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

</script>

<html  >
  <head runat="server">
    <title>TreeView TreeNodePopulate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeView TreeNodePopulate Example</h3>

      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        EnableClientScript="false"
        OnTreeNodePopulate="PopulateNode"
        runat="server">

        <Nodes>

          <asp:TreeNode Text="Inventory"
            SelectAction="Expand" 
            PopulateOnDemand="true"/>

        </Nodes>

      </asp:TreeView>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

   



4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Sep 2008, 06:38 AM
Hi Jimi,

Go through the following help article which explains how to load RadTreeView from Sql tables.
Loading Related SQL Tables

Shinu.
0
jimi
Top achievements
Rank 1
answered on 02 Sep 2008, 08:09 AM
Hi Shinu,

I followed the code and use this ASPX body tag, there's no compile error but
it loads nothing:

Any Idea?


<

body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<div>

<telerik:RadTreeView ID="RadTreeView1" Runat="server" OnNodeExpand ="GenerateTreeView">

<CollapseAnimation Type="OutQuint" Duration="100"></CollapseAnimation>

<ExpandAnimation Duration="100"></ExpandAnimation>

</telerik:RadTreeView>

</div>

</form>

</

body>

0
jimi
Top achievements
Rank 1
answered on 04 Sep 2008, 01:59 PM
Hi Everyone,


I would appreciate it very much if anyone can take a look and tell me what's wrong with the sample application I made following Shinu given link.

Please download here

Thanks,


Jimi

0
jimi
Top achievements
Rank 1
answered on 10 Sep 2008, 01:16 PM
Shinu,

There must be a bug in TreeView for ASP.NET Ajax implementation following this link: Loading Related SQL Tables

Has anyone successfully implemented the same?


Thanks,


Jimi


Tags
TreeView
Asked by
jimi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
jimi
Top achievements
Rank 1
Share this question
or