0

I have a problem with a ComboBox, its items are being duplicated. How can I prevent this situation? Here is some images:

enter image description here

enter image description here

Here is my code sample used to populate it:

string SelectQuery = "SELECT   Part , Model  FROM ctautoparts.nissanpart , ctautoparts.nissan   ;";

MySqlConnection sqlConn = new MySqlConnection(myConnectionString);
MySqlCommand cmdDatabase = new MySqlCommand(SelectQuery, sqlConn);
MySqlDataReader myReader;

try
{
    sqlConn.Open();
    myReader = cmdDatabase.ExecuteReader();
    // int model = myReader.GetOrdinal("model");
    //int model1 = myReader.GetOrdinal("part");
    while (myReader.Read())
    {

    //  string namethestore = myReader.IsDBNull(model)
    //   ? string.Empty
    //  : myReader.GetString("model");

    //   this.carmodel.Text = namethestore;

    //  string namethestore1 = myReader.IsDBNull(model)
    //  ? string.Empty
    // : myReader.GetString("parts");

    ///  this.carpart.Text = namethestore1;
    carmodel.Items.Add(myReader["Model"].ToString());

    carpart.Items.Add(myReader["Part"].ToString());

    }
}
catch (Exception msg)
{
    MessageBox.Show(msg.Message);
}
1
  • When you run the query from a native mysql prompt, do you get duplicates? Commented May 31, 2017 at 19:49

2 Answers 2

1

Add distinct to your select query, so that the duplicates will be removed

Sign up to request clarification or add additional context in comments.

5 Comments

its still duplicate
How are you sure the problem is the query? What if OP is calling that function multiple times?
what you mean OP? sorry im new
@SigbinPInos "Original Poster", take a look here.
how can i stop calling them multiple times?
0

It looks your query is returning duplicated lines. I'd suggest executing them separatedely:

try
{
    // POPULATE MODELS
    string modelsQuery = "SELECT Model FROM ctautoparts.nissan;";
    using (MySqlConnection sqlConn = new MySqlConnection(myConnectionString))
    {
        using (MySqlCommand cmdDatabase = new MySqlCommand(modelsQuery, sqlConn))
        {
            sqlConn.Open();
            MySqlDataReader myReader = cmdDatabase.ExecuteReader();
            // int model = myReader.GetOrdinal("model");
            //int model1 = myReader.GetOrdinal("part");
            while (myReader.Read())
            {
                carmodel.Items.Add(myReader["Model"].ToString());
            }
        }
    }


    // POPULATE PARTS
    string partsQuery = "SELECT Part FROM ctautoparts.nissanpart;";
    using (MySqlConnection sqlConn = new MySqlConnection(myConnectionString))
    {
        using (MySqlCommand cmdDatabase = new MySqlCommand(partsQuery, sqlConn))
        {
            sqlConn.Open();
            MySqlDataReader myReader = cmdDatabase.ExecuteReader();
            // int model = myReader.GetOrdinal("model");
            //int model1 = myReader.GetOrdinal("part");
            while (myReader.Read())
            {
                carpart.Items.Add(myReader["Part"].ToString());
            }
        }
    }
}
catch (Exception msg)
{
    MessageBox.Show(msg.Message);
}

5 Comments

string SelectQuery = "SELECT Part , Model FROM ctautoparts.nissanpart , ctautoparts.nissan ;"; <-- thats my query
@SigbinPInos I edited my answer, I hadn't see your query. It's better and simple to execute two queries instead of only one, since you're not even doing an INNER JOIN.
what does this mean? Severity Code Description Project File Line Error CS0246 The type or namespace name 'SqlDataReader' could not be found (are you missing a using directive or an assembly reference?) CTAUTOPARTS c:\users\kevin\documents\visual studio 2015\Projects\CTAUTOPARTS\CTAUTOPARTS\Addinvetory.cs 145
@SigbinPInos I forgot you're using MySQL. I edited my answer, just change all SqlConnection, SqlCommand and SqlDataReader to MySqlConnection, MySqlCommand and MySqlDataReader (respectively).
BRO! THANK YOU SO MUCH!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.