Database
  Home arrow Database arrow Page 3 - TreeView Part 1 - Populating the New TreeV...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2005-06-08

    Table of Contents:
  • TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table
  • Step 1 – Drag ‘n Drop
  • Step 2 - Populating the Data
  • Step 3 - Building the Tree

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table - Step 2 - Populating the Data


    (Page 3 of 4 )

    Open your code behind file. I will be using C# in this tutorial, but you should have no trouble converting the code to VB.Net if you need to. First let’s reference the proper namespaces, and create a DataSet to hold the data.

    using System;
    using System.Data;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Caching;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    DataSet EmployeeDS = new DataSet();

    Now, we need to configure the page to pull the data on first load, dump it into the DataSet, then into the TreeView. The best way to accomplish this is by separating out the data pull and tree population into two separate methods. The two primary reasons for this are:

    The method for populating the TreeView is recursive. We only need to hit the database once, so we would have to build expensive conditional logic into the recursive method for that to happen. It’s easiest to create a separate function to pull the data and only call it once.

    If there is an error in either step, it’s far easier to isolate if we have separate, distinct functions to debug.

    So here’s the code to do this on the first page load:

    void page_load(object sender, EventArgs e)
    {
            if (!Page.IsPostBack)
            {
                   // first load DataSet
                   LoadDataSet();

                   // now build list
                   BuildEmployeeList(EmployeeList.Nodes, 0);
            }
    }

    Now here’s the code to load the DataSet. It’s pretty basic stuff, so I won’t go into any deep explanation. Just to explain, I’m assuming that you’re storing your connection string in the web.config file as is the best practice, and that’s where the ‘conn’ variable is reading from.

    void LoadDataSet()
    {
       
    String conn =
            ConfigurationSettings.AppSettings["connStr"];

        String strSQL =
            
    "SELECT id, name, mgr_id FROM [employees]";

        SqlDataAdapter myAdapter =
           
    new SqlDataAdapter(strSQL, conn);

        myAdapter.Fill(EmployeeDS, "employees");
    }

    So now we have all the necessary employee data in the DataSet. You can feel free to modify that step all you need to, build in whatever error handling logic and connection closing steps you’d like.  Now we’ll get into the recursive function to populate the tree.

    More Database Articles
    More By Justin Cook


       · It says in the article "you will have no trouble converting it into VB".Wrong. ...
       · A waste of time??The aritcle describes the process and principles behind the...
       · very good article, thanks very much :)
       · It is a good article but very high level cause was hoping to see more complex...
       · what does the mgr_id mean? please help
       · Was a wonderful article and it really helped me.Thank you. Was wondering if we...
       · 1: I never knew C-sharp had a project much more complex than this and converted it...
       · I get an error that specifies that it can't find EmployeeListin the Page_Load...
     

    DATABASE ARTICLES

    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT