Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Adding Data in Treeview from database

Adding Data in Treeview from database

Scheduled Pinned Locked Moved ASP.NET
databasehelpdata-structures
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dj rock
    wrote on last edited by
    #1

    Hi i am trying to add data in tree view control from database, I have folders and documents & sub folders in those folders. so i want to show hierarchy of folders in treeview. my code is as under Here I have table called folder and in this table unique key is ID and parent key is ParentFolderID which reference to Id of the same table so if there are sub folders then ParentFolderId will be parent Folder's ID. So if there is table which is root level then its ParentFolderID will be reference to itself. Try RootFolderStr = "Select Name from Folder where ID=ParentFolderID" dr = temp.ExecuteDataReader(RootFolderStr) While dr.Read Root = dr("Name") tNode.Text = Root Tree1.Nodes.Add(tNode) End While Catch ex As Exception MsgBox(ex.Message) Finally dr.Close() End Try when i am adding one item it works fine but if there are more items then it returns error "Index must be within the bounds of the List.Parameter name: index" So please help if any one knows the answer. Thanks in advance

    A 1 Reply Last reply
    0
    • D dj rock

      Hi i am trying to add data in tree view control from database, I have folders and documents & sub folders in those folders. so i want to show hierarchy of folders in treeview. my code is as under Here I have table called folder and in this table unique key is ID and parent key is ParentFolderID which reference to Id of the same table so if there are sub folders then ParentFolderId will be parent Folder's ID. So if there is table which is root level then its ParentFolderID will be reference to itself. Try RootFolderStr = "Select Name from Folder where ID=ParentFolderID" dr = temp.ExecuteDataReader(RootFolderStr) While dr.Read Root = dr("Name") tNode.Text = Root Tree1.Nodes.Add(tNode) End While Catch ex As Exception MsgBox(ex.Message) Finally dr.Close() End Try when i am adding one item it works fine but if there are more items then it returns error "Index must be within the bounds of the List.Parameter name: index" So please help if any one knows the answer. Thanks in advance

      A Offline
      A Offline
      Abhishek Joshi
      wrote on last edited by
      #2

      Include Microsoft.Web.UI.WebControls.dll in your project. For runtime addition of nodes for TreeView control do following steps //On aspx page register TreeView as <%@ Page language="c#" Codebehind="TreeControl.aspx.cs" AutoEventWireup="false" Inherits="UserControlApplication.TreeControl" %> //Treeview Control //Add the root node for User Control Tree1 in .cs file TreeNode tNode = new TreeNode(); tNode.Text="Root"; tree1.Nodes.Add(tNode); //Add Child Node to root node tNode TreeNode tChileNode1 = new TreeNode(); tChileNode1.Text = str;//This string is the information retrieved form database tNode.Nodes.Add(tChileNode1);

      #Abhi#

      D 1 Reply Last reply
      0
      • A Abhishek Joshi

        Include Microsoft.Web.UI.WebControls.dll in your project. For runtime addition of nodes for TreeView control do following steps //On aspx page register TreeView as <%@ Page language="c#" Codebehind="TreeControl.aspx.cs" AutoEventWireup="false" Inherits="UserControlApplication.TreeControl" %> //Treeview Control //Add the root node for User Control Tree1 in .cs file TreeNode tNode = new TreeNode(); tNode.Text="Root"; tree1.Nodes.Add(tNode); //Add Child Node to root node tNode TreeNode tChileNode1 = new TreeNode(); tChileNode1.Text = str;//This string is the information retrieved form database tNode.Nodes.Add(tChileNode1);

        #Abhi#

        D Offline
        D Offline
        dj rock
        wrote on last edited by
        #3

        hi abhi i have try this but it will work when i have only one row. if there are more rows that i m fetching then error is coming that i have asked in my previous question so if there is another way to solve it please let me know Thanks

        A 1 Reply Last reply
        0
        • D dj rock

          hi abhi i have try this but it will work when i have only one row. if there are more rows that i m fetching then error is coming that i have asked in my previous question so if there is another way to solve it please let me know Thanks

          A Offline
          A Offline
          Abhishek Joshi
          wrote on last edited by
          #4

          Hi Try this - Define your chile node inside while loop and fill it and after it add to root node. :->:->:->

          #Abhi#

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups