This is a migrated thread and some comments may be shown as answers.

Biuld TreeView from sql path

2 Answers 54 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Herman
Top achievements
Rank 1
Herman asked on 17 Nov 2011, 01:31 PM
I want to build a RadTreeView out of a sqldatabase with TreeView Path data
Here is some of the sql data

Standard Service Catalogues
Standard Service Catalogues,IT Outsourcing Catalogue
Standard Service Catalogues,IT Outsourcing Catalogue,Architecture Services
Standard Service Catalogues,IT Outsourcing Catalogue,Architecture Services,Technical Architecture Services
Standard Service Catalogues,IT Outsourcing Catalogue,Architecture Services,Technical Architecture Services,In
Standard Service Catalogues,IT Outsourcing Catalogue,Architecture Services,Technical Architecture Services,Out
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,Ad-hoc Application Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,Business Intelligence Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,Database Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,Middleware Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,SharePoint Application Services
Standard Service Catalogues,IT Outsourcing Catalogue,Business Application Services,SAP Basis Services

2 Answers, 1 is accepted

Sort by
0
Herman
Top achievements
Rank 1
answered on 18 Nov 2011, 11:42 AM
Here is my code how i got this to wrok in normal Treeview but in a RadTreeview I get this error

See screenshot

How do i fix this ?????


Here is the code
SqlCommand DBCmd = new SqlCommand();
 
protected void Page_Load(object sender, EventArgs e)
{
 
}
protected void Button1_Click(object sender, EventArgs e)
{
    TreeView1.Nodes.Add(BuildnodeList());
 
     
}
private TreeNode BuildnodeList()
{
    Dictionary<string, string> nodeListFromDb = new Dictionary<string, string>();
    Connection CN = new Connection();
    if (CN.OpenConnection("MASTER") != "")
    {
        //to do: add javascript alert
    }
    else
   
    {
 
        DBCmd = new SqlCommand("Select_Full_TreeView", CN.sqlConn);
        DBCmd.CommandType = CommandType.StoredProcedure;
 
    }
    SqlDataReader rdr = DBCmd.ExecuteReader();
    while (rdr.Read())
    {
        nodeListFromDb.Add(rdr[1].ToString(), rdr[2].ToString());
    }
    CN.CloseConnection();
 
    TreeNode Node = null;
    foreach (var nodeItem in nodeListFromDb)
    {
        string[] vals = nodeItem.Value.Split(',');
 
        //Create Root
        if (Node == null && vals.Length == 1)
        {
            Node = new TreeNode(vals[0], nodeItem.Key);
            continue;
        }
 
        if (Node == null)
            break;
 
        TreeNode parentNode = GetParentNode(Node, vals[vals.Length - 2]);
        if (parentNode != null)
            parentNode.ChildNodes.Add(new TreeNode(vals[vals.Length - 1], nodeItem.Key));
    }
    return Node;
 
}
 
private TreeNode GetParentNode(TreeNode Node, string Path)
{
    if (String.Equals(Node.Text, Path))
        return Node;
 
    foreach (TreeNode childNode in Node.ChildNodes)
        return GetParentNode(childNode, Path);
 
    return null;
}

0
Plamen
Telerik team
answered on 22 Nov 2011, 12:31 PM
Hello Herman,

I've reviewed the code and I've noticed that you use TreeNode in your logic and the RadTreeView.Nodes.Add method accept as parameters RadTreeNode.

Hope this will help you. 

Greetings,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
TreeView
Asked by
Herman
Top achievements
Rank 1
Answers by
Herman
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or