Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :)

my database:
http://aspalliance.com/images/articleimages/732/image001.jpg[^]

webform.aspx.cs:
C#
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

1 solution

Error Message: Specified cast is not valid.
C#
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:
C#
tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);
 
Share this answer
 
v2

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