Search code examples
c#mysqlwinformscombobox

Eliminating Duplicates from combobox C#


Greetings i am facing this problem where i need to remove any duplicates that are being populated from mysql database into the combobox.

this is my code:

void fillcatagory()
    {
        string query = "select * from CatagorieTable";
        MySqlCommand cmd = new MySqlCommand(query, Con);
        MySqlDataReader rdr;
        try
        {
            Con.Open();
            DataTable dt = new DataTable();
            dt.Columns.Add("Catagname", typeof(string));
            rdr = cmd.ExecuteReader();
            dt.Load(rdr);
            catcombo.ValueMember = "Catagname";
            catcombo.DataSource = dt;
            searchcombo.ValueMember = "Catagname";
            searchcombo.DataSource = dt;
            Con.Close();
        }
        catch
        {

        }
    }

Solution

  • Update your query to return unique category names like so:

    string query = "select distinct Catagname from CatagorieTable";
    

    Since you're only using the one column, there's no reason to select anything else.