How can I avoid duplicate items in a checkListBox

RedRocket

I am working on my windows form application using c#. I have a checkListBox that is binded to db. I am wondering is there a way to delete any duplicate record from the database?

Here is my code

 private void fill_checkListBox()
 {
     try
     {
         string query = "select * from table_1 ";

         SqlCommand myTeacherCommand = new SqlCommand(query, myConn);

         //reading the value from the query
         dr = myCommand.ExecuteReader();
         //Reading all the value one by one
         teacherCB.Items.Clear();


         while (dr.Read())
         {
             string name = dr.IsDBNull(2) ? string.Empty : dr.GetString(2);

             teacherCB.Items.Add(name);

             if (!checkBox.Items.Contains(name))
             {
                  teacherCB.Items.Add(name);
             }           
         }
         dr.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Backs

The first answer - use DISTINCT in query:

select distinct * from table_1

Also, I advice you to specify column names in query:

select distinct ID, Name from table_1

But I don't know anything about your data in table.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I avoid duplicate templates in Meteor?

How I can avoid duplicate records for insert

How can I avoid duplicate switch statements

How can I avoid the duplicate code of mapping in Spring project?

How can I avoid auto-incrementing on update of duplicate key?

How can I avoid duplicate SQL queries with Django model forms?

How can i avoid duplicate request of button click twice

How can I avoid duplicate digit in the cicle result

How can I avoid having duplicate data in my database?

How can i simplify these methods to avoid duplicate code?

How can I cache items in RxJava and avoid cache stampede?

C#: How can i store the values selected in checklistbox to an array

in Android java, how can I remove duplicate items in a list?

How can I delete duplicate items in ArrayList <Object> with LinkedHashSet?

Can I avoid duplicate strings with the sed "a\" command?

How to get the checked items string in a CheckListBox

How can I avoid duplicate code when I use sort() in JavaScript to put Ascending/Descending options (react)?

How can I avoid duplicate code when I create a query builder?

How to Bind Checked / Selected Items From CheckListBox in WPF by using MVVM Model (I am using "WPFToolkit.Extended.dll" to get CheckListBox Control)

how to avoid duplicate items with ng-repeat and drop in angularjs?

Using Spring Data,Mongodb, how can I avoid Duplicate vertices error

How can I avoid reprocess duplicate messages in a pub/sub when the k8s make HPA?

How can I avoid duplicate rows in foreign table with EF Code First

How can I avoid duplicate random three-word combinations from a table of words?

How can I avoid duplicate values when loading data using a spring batch job?

How can I avoid duplicate code with the same method body but different return types in Java?

How can I avoid duplicate background task processing in Service Fabric hosted services?

How can I avoid duplicate rows from near-simultaneous SQL adds?

How can I fix my code to avoid returning duplicate pairs while using map in racket?