3 Replies - 750 Views - Last Post: 21 August 2013 - 01:10 AM Rate Topic: -----

#1 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Remove repeating items in checked listbox

Posted 16 August 2013 - 09:13 AM

How do I remove repeating items from a checked list box? I'm populating it from my database. In my database, I have some items with the same name, but different item number. Example: ItemName - Ball, ItemNbr - 123; Ball, 234; etc.
I just want to list only one "Ball" or have it list as "Ball, 123", "Ball, 234".

This the code I'm using to populate the checked list box:
Dim Connection As New SqlConnection("Data Source = Server; Initial Catalog=DatabaseName; Integrated Security=SSPI")
        Dim ItemSelect As SqlCommand = New SqlCommand("SELECT ItemName FROM Item", Connection)
        Dim NumberSelect As SqlCommand = New SqlCommand("SELECT ItemNbr FROM Item", Connection)
 Dim Rdr As SqlDataReader

 'Populate ItemCheckedListBox with ItemName Data
        Connection.Open()
        Rdr = ItemSelect.ExecuteReader()
        ItemCheckedListBox.BeginUpdate()
        ItemCheckedListBox.Items.Clear()
        While (Rdr.Read())
            ItemCheckedListBox.Items.Add(Rdr.GetString(0))
                    End While
        Rdr.Close()
        Connection.Close()
        ItemCheckedListBox.EndUpdate()

        'Populate NumberCheckedListBox with ItemNbr from Database
        Connection.Open()
        Rdr = NumberSelect.ExecuteReader()
        NumberCheckedListBox.BeginUpdate()
                While (Rdr.Read())
            NumberCheckedListBox.Items.Add(Rdr.GetValue(0))
        End While
        Rdr.Close()
        Connection.Close()
        NumberCheckedListBox.EndUpdate()
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Remove repeating items in checked listbox

#2 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,106
  • Joined: 11-May 12

Re: Remove repeating items in checked listbox

Posted 16 August 2013 - 09:19 AM

Where you are adding them in on line 12 put a statement that if the string already exists don't add it like so

While (Rdr.Read())
If Not ItemCheckedListBox.Items.Contains(Rdr.GetString(0))
ItemCheckedListBox.Items.Add(Rdr.GetString(0))
End If
End While



And if needed do the same with the second While loop. I do have a question though. Why aren't you just getting both values in one query instead of connecting to the database 2 times and executing 2 queries?

This post has been edited by ybadragon: 16 August 2013 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Remove repeating items in checked listbox

Posted 19 August 2013 - 06:47 PM

I tried that added code, but it didn't work. I'm still getting multiple repeating item numbers. How do I combine the two in one query and have it display as "ItemName, ItemNbr" in my checked list box?
Was This Post Helpful? 0
  • +
  • -

#4 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: Remove repeating items in checked listbox

Posted 21 August 2013 - 01:10 AM

Do this

dim i as integer = 0
dim k as integer = 0
dim value1 as string =""
dim value2 as string = ""

while k < listbox.items.count

   value1= item in postion 1 of the listbox (k)

   while i < listbox.items.count
    
        value2= item in the 2nd position of the listbox (i)

        if k = i then
           'do nothing
        else
             if value1 = value2 then
             remove the item
        end if
 
      i=i+1

   end while

   k=k+1

end while



something like that, im not at a compiler so i couldnt test.

The idea is loop the list twice, 1 to run the compare against the value to be compared and then move to the next position

im sure you get the point

:)/>

This post has been edited by deery5000: 21 August 2013 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1