Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to load treeview dynamically but when i run the project windows shows empty.Please review my code and tell me where i have mistaken.
public partial class Form1 : Form
{
	TreeNode parentNode = null;
	public Form1()
	{
		InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
		//TreeNode node = new TreeNode("Windows");
		using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
		{
			using (SqlDataAdapter adapt = new SqlDataAdapter("Select * from TreeviewTable", con))
			{
				DataTable dt = new DataTable();
				adapt.Fill(dt);
				foreach (DataRow row in dt.Rows)
				{
					parentNode = treeView1.Nodes.Add(row["NodeName"].ToString());
					PopulateTreeview(Convert.ToInt32(row["TreeviewId"].ToString()), parentNode);
				}

				treeView1.ExpandAll();
			}
		}
	}

	private void PopulateTreeview(int _parentId, TreeNode _parentnode)
	{
		using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
		{
			using (SqlDataAdapter adapt = new SqlDataAdapter("Select * from dbo.SubMenuTable Where ParentId=" + _parentnode, con))
			{
				DataTable dt = new DataTable();
				adapt.Fill(dt);
				TreeNode childnode;
				foreach (DataRow row in dt.Rows)
				{
					if (_parentnode == null)
						childnode = treeView1.Nodes.Add(row["SubMenuName"].ToString());
					else
						childnode = _parentnode.Nodes.Add(row["SubMenuName"].ToString());
					PopulateTreeview(Convert.ToInt32(row["SubMenuId"].ToString()), childnode);
				}
			}
		}
	}
}
Posted
Updated 18-Nov-14 23:42pm
v2
Comments
BillWoodruff 19-Nov-14 5:48am    
0. no errors: correct ?

1. Is it possible that the DataSet is empty ? Put a break-point here and examine the contents of 'dt

adapt.Fill(dt);
foreach (DataRow row in dt.Rows)

2. are any nodes actually being created: put a break-point on

parentNode = treeView1.Nodes.Add(row["NodeName"].ToString());
PopulateTreeview(Convert.ToInt32(row["TreeviewId"].ToString()), parentNode);

and examine the contents of each node created: does the NodeName show up as expected ?

1 solution

ajays3356534 wrote:

C#
using (SqlDataAdapter adapt = new SqlDataAdapter("Select * from dbo.SubMenuTable Where ParentId=" + _parentnode, con))


First problem: You're trying to pass _parentnode to the query. I suspect you meant to pass _parentId instead.

Second problem: NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Although in this particular case, since you're passing an int, your code won't be vulnerable to SQL Injection[^], you should still avoid using string concatenation. Otherwise, you'll forget and concatenate a user-supplied string; or someone else will copy your code without realising the caveat.

C#
using (SqlDataAdapter adapt = new SqlDataAdapter("Select * from dbo.SubMenuTable Where ParentId = @ParentId", con))
{
    adapt.SelectCommand.Parameters.AddWithValue("@ParentId", _parentId);
    ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900