We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 119,099 times

Contents

Downloads

Related Categories

Tree structures in ASP.NET and SQL Server - Displaying our Nodes

Displaying our Nodes

Now that we've got the basics out of the way, we're in a position to start thinking about what we actually need to display. The first obvious step is to write some functionality that lets us view nodes at a particular level. For example - if we're at the root of our tree, we want an ASP.NET page that can display its children.

The SQL to fetch the direct children of a particular node is very simple:

CREATE PROCEDURE dfTreeGetChildren ( @id INT ) AS
SELECT * FROM dfTree WHERE parentId=@id

This stored procedure will effectively return a list of TreeNode objects - so in our SqlServerTreeProvider class we create a ProcessList procedure that accepts a stored procedure and array of parameters - and returns an ArrayList containing the nodes we're interested in. We can then use this standard procedure for executing dfTreeGetChildren. Note we've also created a TreeNodeFromDataReader method that takes a SqlDataReader object, and returns a TreeNode by reading the appropriate fields.

public ArrayList GetChildren(int uniqueID)
{
    return ProcessList("dfTreeGetChildren",
        new SqlParameter("@id",uniqueID));
}
protected ArrayList ProcessList(string storedProcedure, params SqlParameter[] parameters) {
    using (SqlConnection sqlConn = new SqlConnection(connectionString) )
    {
        // execute the appropriate sp
        SqlCommand sqlComm = new SqlCommand(storedProcedure,sqlConn);
        sqlComm.CommandType = CommandType.StoredProcedure;
        foreach(SqlParameter param in parameters)
            sqlComm.Parameters.Add(param);
       
        ArrayList nodes = new ArrayList();
        sqlConn.Open();
        SqlDataReader dr = sqlComm.ExecuteReader();
        while (dr.Read())
            nodes.Add ( TreeNodeFromDataReader(dr) );
        dr.Close();
        sqlConn.Close();
        return nodes;
    }
}
protected virtual TreeNode TreeNodeFromDataReader(SqlDataReader dr)
{
    TreeNode node = new TreeNode(
        (int)dr["id"],
        (string)dr["name"],
        dr["parentId"] == DBNull.Value ? 0 : (int)dr["parentId"],
        (int)dr["depth"]);
    return node;
}

Now that's sorted, we can get started on our ASP.NET page. For the moment, this will consist of a single Repeater control, that we will bind to the ArrayList returned by the GetChildren method we defined above.

TreeDemo.aspx

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD><title>Tree Demo</title>
</HEAD>
<body>
    <form id="Form1" method="post" runat="server">
        <asp:Repeater Runat="server" ID="categoryList">
            <ItemTemplate>
                <a href='<%# "TreeDemo.aspx?id=" + DataBinder.Eval(Container.DataItem,"UniqueID") %>'>
                    <%# DataBinder.Eval(Container.DataItem,"Name") %></a>
            </ItemTemplate>
            <SeparatorTemplate> <br /> </SeparatorTemplate>
        </asp:Repeater>
    </form>
</body>
</HTML>

 

This is the code we'll use in the code-behind.

private void Page_Load(object sender, System.EventArgs e)
{
    // we could easily implement alternate stored procedures that
    // let us fetch a "default" root item id depending on its name
    int treeID = 1; // default to some tree or other
    if (Request.QueryString["id"]!=null)
        treeID = Int32.Parse(Request.QueryString["id"]);
    SqlServerTreeProvider treeProvider = new SqlServerTreeProvider();
    categoryList.DataSource = treeProvider.GetChildren(treeID);
    categoryList.DataBind();
}

Now, supposing we created some nodes as follows:

SqlServerTreeProvider treeProvider = new SqlServerTreeProvider();
int indexID = treeProvider.AddNode(new TreeNode("Index",0));
int langID = treeProvider.AddNode(new TreeNode("Languages",indexID));
treeProvider.AddNode(new TreeNode("C#",langID));
treeProvider.AddNode(new TreeNode("VB.NET",langID));
treeProvider.AddNode(new TreeNode("Technologies",indexID));

and then visited TreeDemo.aspx?id=#langID#, we would see something like this:

A diagram displaying the structure of the table, with these fields: id (int, primary key), parentId (int), name (varchar), depth (int), lineage (varchar).

Not exactly impressive really, is it?! However, it's a start. It would be more useful if we could know where we actually are within the tree - so, next up is providing some "breadcrumbs".

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [4633] Tree structures in ASP.NET and SQL Server

    Posted by toranaga on 11 Mar 2007

    Here is how id did it to use with asp:tree:

    add this methods to the TreeNode class

    public string GetXml
        {
            get
     ...

  • Re: [4633] Tree structures in ASP.NET and SQL Server

    Posted by Goose99 on 24 Apr 2006

    Thanks this article has been a great help, I have one question.


    How hard would it be to get the tree f...

  • Nestedet model

    Posted by jcelko on 04 Nov 2005

    Here is my standard "cut & paste" on the Nested sets model for hierarchies.

    There are many ways to represent a tree or hierarchy in SQL. This is called an adjacency list model and it looks like th...

  • Posted by James Crowley on 03 Nov 2005

    Okay, point taken ... but as far as I'm aware if you moved the code I talk about out of triggers just into stored procs (or whatever), then the SQL is pretty standard stuff?

    I know that you're a ve...

  • Posted by jcelko on 03 Nov 2005

    Yoiu can do your heirarchies & trees in pure declarative SQL without using any proprieary 4GL like PL/SQL, T-SQL, etc.