2
Reply

treeview in visual studio 2012

Devendra Dighe

Devendra Dighe

Feb 6 2014 2:08 AM
1.2k
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)
here is my c# code:
 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);
}
}
}
 and .aspx code for tree view
<asp:TreeView
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" />

It shows blank screen in output .plz help me . thank you 


Answers (2)