0

I have combobox items to display date of draw. Items are imported from mysql data. I use this code to get them:

        DataTable dateDraw = new DataTable("dateDraw");
        using (MySqlConnection sqlConn = new MySqlConnection(@"datasource=localhost;port=3306;username=root;password=toor"))
        {
            using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT date FROM sqldata.datedraw WHERE active='Yes'", sqlConn))
            {
                da.Fill(dateDraw);
            }
        }
        foreach (DataRow da in dateDraw.Rows)
        {
            comboBox1.Items.Add(da[0].ToString());
        }

I need items to refresh every second. So I created timer and place code into timer. Set timer.enable to true and interval to 1000. The refresh part works OK. But I get multiple items with same results duplicating on each mysql query.

If mysql first query is for example:

12.9.2013
19.9.2013
16.9.2013

The second query duplicates it like:

12.9.2013
19.9.2013
16.9.2013
12.9.2013
19.9.2013
16.9.2013

and the third query:

12.9.2013
19.9.2013
16.9.2013
12.9.2013
19.9.2013
16.9.2013
12.9.2013
19.9.2013
16.9.2013

and so on.

How to remove duplicate results?

Thank you.

I am looking for solution I will understand because I am a noob. Thanks!

1
  • 1
    Do a comboBox1.Items.Clear(); right before you add new items.
    – NoLifeKing
    Nov 15, 2013 at 12:59

3 Answers 3

2

You can clear comboBox before adding items, Am I missing something?

comboBox1.Items.Clear();
0
2

add

  comboBox1.Items.Clear();

before

  foreach (DataRow da in dateDraw.Rows)
    {
        comboBox1.Items.Add(da[0].ToString());
    }
0
0

Instead of adding them row by row, you could use

comboBox1.ItemsSource = dateDraw.Rows

You can set seperately which fields in those rows should be used for display and as (key)value setting combobox1.DisplayMember and combobox1.Datamember respectively

Apart from that, as a user i might get homocidal intension towards the programmer if i am selecting something from a combobox, and that combobox goes empty and refills, probably making my selection disappear - if I ever manage to make a selection at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.