Dynamic fill Treeview from (msaccess) database

Last post 09-23-2009 10:17 AM by vaibh. 1 replies.

Sort Posts:

  • Dynamic fill Treeview from (msaccess) database

    07-27-2005, 2:17 PM
    • Member
      5 point Member
    • yoska
    • Member since 07-27-2005, 6:05 PM
    • Posts 1
    After some searching and putting code together from many places i finaly got it to work: dynamic fill a tree by using a Populate on demand and using the default treeview control in ASP.Net 2.0. In many examples on the web when using populateondemand you 'll find a +-sign in front of every node. This code only shows a +-sign in front of a node when the node has children.

    First create a table (i've use msaccess, but you can also use SQL Sever) named tree
    Create tree fields

    id = autonumber
    name = text
    parentid=integer

    fill is with data you like, eg. (not the best example but it will do). Remeber the location where you've put your db. You will need it later!

    id               name            parentid
    1               MyPC           0 
    2               Mouse           1
    3               Keyboard      1
    4               Sound            1
    5               Subwoofer     4
    6               Speaker 1      4
    7               Speaker 2      4

    After creating the table and filling it with data it's time to do some coding

    Open Visual Web developer 2005 and create a new project
    Insert a Treeview component on the default.aspx form and leave the name 'treeview1'
    You can create any style you like.

    In the default.aspx.vb page, copy the following code:
    Only thing you need to change is the database location.
    ==== Code start ========

    Partial Class _Default
    Inherits
    System.Web.UI.Page

    Dim oleConn As System.Data.OleDb.OleDbConnection
    Dim oleSQL As
    System.Data.OleDb.OleDbCommand
    Dim oleSQLf As
    System.Data.OleDb.OleDbCommand
    Dim oleDR As
    System.Data.OleDb.OleDbDataReader
    Dim oleDRf As
    System.Data.OleDb.OleDbDataReader
    Dim strConnection As
    String

    Function
    hasChildren(ByVal iNode As Integer) As Boolean

       'Check if a node will have children. This to preven +-signs at all nodes
       'if a node has children, return true. Else false

       oleSQLf = New System.Data.OleDb.OleDbCommand("Select * from tree where parentid=" & iNode, oleConn)

        'Fill data reader
       oleDRf = oleSQLf.ExecuteReader()

         If oleDRf.HasRows = True Then
             hasChildren =
    True
        Else
             hasChildren =
    False
       End If

    End Function

    Sub dbConnect()

    'Create db connection
    'CHANGE THE CONNECTIONS STRING

      strConnection = ConfigurationManager.AppSettings("dbConnectionString"
      oleConn = New System.Data.OleDb.OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & strConnection & ""
    )

    End Sub

    Sub fill_Tree()

    'connect to database
    dbConnect()

    'Create root Query
    oleSQL = New System.Data.OleDb.OleDbCommand("Select * from tree where parentid=0"
    , oleConn)

    'Open database connection
    'Fill data reader

    oleConn.Open()
    oleDR = oleSQL.ExecuteReader()

    'Fill tree with root nodes
    Dim nRoot As
    TreeNode

    While oleDR.Read()
        nRoot = New TreeNode

        With nRoot
                .Text = oleDR(1).ToString
                .Value = oleDR(0).ToString
                .NavigateUrl =
    http://www.asp.net/
                .Target = "_blank"
                .PopulateOnDemand =
    "true"

       End With
       TreeView1.Nodes.Add(nRoot)

    End While

    'Close db connection

    oleConn.Close()

    end Sub

    Protected Sub TreeView1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.Init

       Call fill_Tree()
       TreeView1.CollapseAll()

    End Sub

    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate

       'Open and connect database 
       dbConnect()
       oleConn.Open()

       'Create Query 
       oleSQL = New System.Data.OleDb.OleDbCommand("Select * from tree where parentid=@parentid"
    , oleConn)
       oleSQL.Parameters.AddWithValue("@ParentID"
    , e.Node.Value)

          'Fille the tree with nodes

          Try   
                oleDR = oleSQL.ExecuteReader()

                While oleDR.Read()
                           Dim newNode As New
    TreeNode()
                           newNode.Text = oleDR("name"
    ).ToString()
                           newNode.Value = oleDR("id"
    ).ToString()
                            newNode.PopulateOnDemand = hasChildren(CInt(oleDR("id"
    )))
                            e.Node.ChildNodes.Add(newNode)

             End While

    Finally

          'close connection
          oleConn.Close()

    End Try

    End Sub

    End Class

    ==== Code end ========


    Hope you like my code. Please let me know what you think.

    regards

    iwan




  • Re: Dynamic fill Treeview from (msaccess) database

    09-23-2009, 10:17 AM
    • Member
      10 point Member
    • vaibh
    • Member since 05-15-2009, 6:08 AM
    • Nagpur
    • Posts 6

    Thank you yoska you did a very good work this logic is very usefull but i am try this in windows application then it is giving me problems of not showing some events if u help me then it will be good for me pm me .


    Thank You again for this

    Vaibhav Mayee
    MCTS Web Application
    Software Developer

    If u like the solution then mark as answer !!
Page 1 of 1 (2 items)