Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MySQL Treeview
i got this website as a reference:
http://aspalliance.com/732_display_hierarchical_data_with_treeview_in_aspnet_20[^]
 
and i convert it to c# with MySQL, but having some problem, please help me have a look Smile | :)
 
my database:
http://aspalliance.com/images/articleimages/732/image001.jpg[^]
 
webform.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            PopulateRootLevel();
    }
 
private void PopulateRootLevel()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["csTest"].ToString();
 
        MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
        MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();
 
        cmd.CommandText = "SELECT id,title,(SELECT COUNT(*) FROM test.treeview WHERE parentid=sc.id) childnodecount FROM test.treeview sc WHERE parentID IS NULL";
 
       MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, TreeView1.Nodes);
    }
 
private void PopulateSubLevel(int parentid, TreeNode parentNode)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["csTest"].ToString();
 
        MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
        MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();
            
        cmd.CommandText = "SELECT id,title,(SELECT COUNT(*) FROM test.treeview WHERE parentid=sc.id) childnodecount FROM test.treeview sc WHERE parentID=@parentID";
            
        cmd.Parameters.Add("?parentID", MySqlDbType.Int32, parentid);
        MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, parentNode.ChildNodes);
    }
 
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["title"].ToString();
            tn.Value = dr["id"].ToString();
            nodes.Add(tn);
 
           //If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0); //error at this line
        }
    }
 
Error Message: Specified cast is not valid.
Posted 9-Jan-13 21:20pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Error Message: Specified cast is not valid.
 tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0); //error at this line
Based on the error, it is pretty clear that the type cast you tried to do is not valid. This means, dr["childnodecount"] is not of datatype integer.
 
Make sure, you have column named as 'childnodecount' in your dataset and then do make sure it is having an integer value before casting it directly into an int. Once you are sure of all, try:
 tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0); 
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100