0
votes

I have a table for Orders (orderTBL). When the user creates an order it adds a new row into the database with a custom order number. I load my treeview nodes from this database, however, if there is more than one row with the same order number, it creates more than one treeview node. Is it possible to display only one treeview node per order number? The TreeView is used to control a DataRowFilter to only display orders with the order number selected in a DataGridView This is the code I use :

public void ordersForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'lWADataBaseDataSet.orderTBL' table. You can move, or remove it, as needed.
      //  this.orderTBLTableAdapter.Fill(this.lWADataBaseDataSet.orderTBL);
        getOrders();
        getNumbers();
        string sOrder = null;
        int I = 0;
        for (I = 0; (I <= (orderTBL.Rows.Count - 1)); I++)
        {


            sOrder = orderTBL.Rows[1][1].ToString();
            treeView1.Nodes[0].Nodes.Add(sOrder);
        }



    }

private void getNumbers()
    {
        SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\LWADataBase.sdf;");
        try

        {

            con.Open();
        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
        treeView1.Nodes.Clear();
        SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM orderTBL ORDER BY[Order Number] ASC", con);

        try
        {

            SqlCeDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                TreeNode node = new TreeNode(dr["Order Number"].ToString());

                treeView1.Nodes.Add(node);

            }
        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message);
            return;

        }
        con.Close();

    }
1
That's normally a very serious bug. Don't hide it, fix it.Hans Passant

1 Answers

2
votes

I am assuming that having more than one row with the same order # is part of your design. If not you should do a check on the database before insertion.

Being part of your design you just want to load one node for each order you can do this in two ways.

Filter the data via the query:

 SqlCeCommand cmd = new SqlCeCommand("SELECT distinct [Order Number] FROM orderTBL ORDER BY[Order Number] ASC", con);

'distinct' tells the database to make sure now row in the return set is duplicated.

Or maintain a temp list during load that checks if you have loaded that order yet.

    private void getNumbers()
    {
        SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\LWADataBase.sdf;");
        try
        {

            con.Open();
        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
        treeView1.Nodes.Clear();
        SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM orderTBL ORDER BY[Order Number] ASC", con);

        try
        {
            //Temp List
            List<string> ordersLoaded = new List<string>();
            SqlCeDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                string oderNum = dr["Order Number"].ToString();
                //Check if you loaded that order already
                if (!ordersLoaded.Contains(oderNum))
                {
                    //Add order to loaded list
                    ordersLoaded.Add(oderNum);
                    treeView1.Nodes.Add(new TreeNode(oderNum));
                }
            }
        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message);
            return;

        }
        con.Close();

    }