I'm getting an "Object reference not set to an instance of an object" error with this line of code:
t1.FindNode(rs.GetString(4)).ChildNodes.Add(node);
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)
{
PopulateTree(SampleTreeView);
}
public void PopulateTree(TreeView t1)
{
// Clear any exisiting nodes
t1.Nodes.Clear();
using (SqlConnection connection = new SqlConnection())
{
// Data Connection
connection.ConnectionString = (ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
connection.Open();
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,
IsNull(
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)
{
t1.Nodes.Add(node);
}
else
{
t1.FindNode(rs.GetString(4)).ChildNodes.Add(node);
}
}
}
}
}
}
}
}
Any help would be appreciated.