Creating a tree from a database

I have a database called dtEmploye with fields

employe_id First
name
last name
boss_id , which has a recursive relation to employee_id , where I have the code XXX

I have some code, but there is some error showing that "The specified cast is invalid."

private void btntree_Click(object sender, EventArgs e)
    {
        DataTable vec = sel("SELECT * FROM dtemployee");
        treeView1.Nodes.Clear();
        foreach (DataRow dr in vec.Rows)
        {
            if ((int)dr["boss_id"] == XXX)
            {
                TreeNode parent = new TreeNode();
                parent.Text = dr["name"+"surname"].ToString();
                string value = dr["employe_id"].ToString();
                parent.Expand();
                treeView1.Nodes.Add(parent);
                sublevel(parent, value);
            }
        }
    }
    public int sublevel(TreeNode parent, string id)
    {
        DataTable ch = sel("SELECT * FROM dtEmploye WHERE boss_id=" + id);
        if (dtEmploye.Rows.Count > 0)
        {
            foreach (DataRow dr in ch.Rows)
            {
                TreeNode child = new TreeNode();
                child.Text = dr["name"+"surname"].ToString().Trim();
                string temp = dr["employe_id"].ToString();
                child.Collapse();
                parent.Nodes.Add(child);
                sublevel(child, temp);
            }
            return 0;
        }
        else
        {
            return 0;
        }
    }
    protected DataTable sel(string select)
    {
        NpgsqlDataAdapter adpt = new NpgsqlDataAdapter(select, con);
        DataTable dt2 = new DataTable();
        adpt.Fill(dt2);
        return dt2;
    }
+3
source share
4 answers

try it

    if ((int)dr["boss_id"] == XXX && DBNull.Value != dr["boss_id"])
    {
        TreeNode parent = new TreeNode();
        parent.Text = dr["name"].ToString() + dr["surname"].ToString();
        string value = dr["employe_id"].ToString();
        parent.Expand();
        treeView1.Nodes.Add(parent);
        sublevel(parent, value);
    }
+1
source

Why not do it in SQL? You can make a recursive query, and then you can add all the nodes by going through the dataset into the code, using sorting to determine which node the subnode belongs to.

;WITH Tree (ID, [NAME], PARENT_ID, Depth, Sort) AS
(
    SELECT ID, [NAME], PARENT_ID, 0 AS Depth, CONVERT(varchar(255), [Name]) AS Sort FROM Category
    WHERE PARENT_ID = 0
    UNION ALL
    SELECT CT.ID, CT.[NAME], CT.PARENT_ID, Parent.Depth + 1 AS Depth, 
    CONVERT(varchar(255), Parent.Sort + ' | ' + CT.[NAME]) AS Sort
    FROM Category CT
    INNER JOIN Tree as Parent ON Parent.ID = CT.PARENT_ID
)

-- HERE IS YOUR TREE, Depths gives you the level starting with 0 and Sort is the Name based path
SELECT ID, [NAME], PARENT_ID, Depth, Sort FROM Tree
ORDER BY Sort
+1
source

, , :

if ((int)dr["boss_id"] == XXX)

- :

if(dr["boss_id"] != null && dr["boss_id"] is int && (int)dr["boss_id"] == XXX)
0

, , . , "". .

:

class Truck : Automobile

class Car : Automobile

Automobile truck = new Truck();
Truck refoftruck = (Truck)truck; //correct usage;

Car badrefofTruck = (Car)truck; //will not work;

, ( ), , . , - .

int. , . , , , int, string, double ..... , , , . .

To convert a string to int (note the word Convert not cast), you should use:

Convert.ToInt32(strObject.ToString());

Hope this helps and clarifies. Of course, you probably need to verify that the object is not null before performing the conversion.

0
source

All Articles