Treeview与mysql [英] Treeview with mysql

查看:126
本文介绍了Treeview与mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此网站作为参考:

http://aspalliance.com/732_display_hierarchical_data_with_treeview_in_aspnet_20 [< a href =http://aspalliance.com/732_display_hierarchical_data_with_treeview_in_aspnet_20target =_ blanktitle =New Window> ^ ]



和i用MySQL将它转换为c#,但有一些问题,请帮我一看:)



我的数据库:

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



webform.aspx.cs:

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:

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.

推荐答案

错误消息:已指定演员无效。

Error Message: Specified cast is not valid.
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0); //error at this line



根据错误,很明显你试图做的类型转换是无效的。这意味着, dr [childnodecount] 数据类型为整数。



确保在数据集中有一个名为childnodecount的列,然后在将其直接转换为int之前确保它具有整数值。一旦你确定了所有,请尝试:


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);


这篇关于Treeview与mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆