I'm getting an "Object reference not set to an instance of an object" error with this line of code:


Here is all of the code:

using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace TreeViewProject
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        public void PopulateTree(TreeView t1)

            // Clear any exisiting nodes

            using (SqlConnection connection = new SqlConnection())
                // Data Connection
                connection.ConnectionString = (ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);

                string getLocations = @"
                    With hierarchy (id, [location id], name, depth, [path])
                    As (

                        Select ID, [LocationID], Name, 1 As depth,
                            Cast(Null as varChar(max)) As [path]
                        From dbo.Locations
                        Where ID = [LocationID]

                        Union All

                        Select child.id, child.[LocationID], child.name,
                            parent.depth + 1 As depth,
                                parent.[path] + '/' + Cast(parent.id As varChar(max)),
                                Cast(parent.id As varChar(max))
                            ) As [path]
                        From dbo.Locations As child
                        Inner Join hierarchy As parent
                            On child.[LocationID] = parent.ID
                        Where child.ID != parent.[Location ID])

                    Select *
                    From hierarchy
                    Order By [depth] Asc";

                using (SqlCommand cmd = new SqlCommand(getLocations, connection))
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rs = cmd.ExecuteReader())
                        while (rs.Read())
                            Guid id = rs.GetGuid(0);
                            Guid locationID = rs.GetGuid(1);
                            TreeNode node = new TreeNode();
                            node.Text = rs.GetString(2);
                            node.Value = id.ToString();

                            if (id == locationID)


Any help would be appreciated.

Edited 5 Years Ago by neoseeker191: n/a

your code: t1.FindNode(rs.GetString(4)).ChildNodes.Add(node);

rs.getstring(4) might be null therefore triggering that exception, OR else tl does not contain the node whose name is from result rs.GetString()\

Check them both starting from rs.GetString(4) - ensure its not null.. by placing a breakpoint, and using immediate window for debugging

Turns out it was grabbing GetString(4) as an uppercase, so it couldn't find a match. Adding ToLower() fixed the issue. Thanks for your help!

This article has been dead for over six months. Start a new discussion instead.