Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, I am using trying to use treeview in visual studio 2012 asp.net4.5,c#, and MySql Database. I took treeview control from toolbox and populated it.
But it does not show any output. it shows blank screen
my database contains two tables table i.e subject(subject_id,subject_name) & topic(topic_id,subject_id,topics)
It shows blank output.there is no error.plz help me with this.
C#
public partial class Study : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}
private void PopulateRootLevel()
{
try
{
MySqlConnection con = new MySqlConnection(@"User Id=root;Host=localhost;Database=helix");
MySqlCommand cmd = new MySqlCommand(@"SELECT topic_id,topics,(SELECT count(*) FROM topic WHERE subject_id=sc.topic_id) childnodecount FROM topic sc WHERE subject_id IS NULL ", con);
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void PopulateSublevel(int subject_id, TreeNode parentNode)
{
try
{
MySqlConnection con = new MySqlConnection(@"User Id=root;Host=localhost;Database=helix");
MySqlCommand cmd = new MySqlCommand(@"SELECT topic_id,topics,(SELECT count(*) FROM topic WHERE subject_id=sc.topic_id) childnodecount FROM topic sc WHERE subject_id=@subject_id ", con);
con.Open();
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = subject_id;
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
con.Close();
}
catch(Exception ex)
{
throw ex;
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSublevel(Int32.Parse(e.Node.Value), e.Node);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["topics"].ToString();
tn.Value = dr["topic_id"].ToString();
nodes.Add(tn);
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}
}

ASP.NET
<asp:TreeView
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" />
Posted
Updated 5-Feb-14 20:54pm
v3
Comments
Sergey Alexandrovich Kryukov 6-Feb-14 2:22am    
TreeView? Which one? Full type name, please.
—SA

1 solution

 
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