Hi, all. I need a little advice on a school web project. I'm using VS2010/VB/ASP

Some background: I have a listbox(LBX) and a dropdown list(DDL). Each are bound to their own AccessDataSource objects, which in turn are bound to different tables in a database. Each is successfully populated from their respective datasources.

What I want to do is compare the items in each, one by one, and remove items from the DDL which are already present in the LBX. The long term idea is that when a user selects an item in the DDL, it will be moved from the DDL to the LBX. There is no point in having an item in the DDL which is already in the LBX.

I don't want to instantly transfer all differing items, I just want to prepare the DDL for the user to select what to transfer. I have a nested For Each loop so far. It isn't giving me any errors, it just isn't removing anything!

For Each item1 In DDL.Items
            For Each item2 In LBX.Items
                If DDL.SelectedItem.ToString = LBX.SelectedItem.ToString Then
                    DDL.Items.Remove(DDL.SelectedItem.ToString)
                End If
            Next
        Next

Can anyone point out what I'm doing wrong? I've looked here Click Here but been unable to determine my error(s).

Thanks!

Recommended Answers

All 2 Replies

You're looping through item1 and item2 to compare each item but then actually comparing DDL.SelectedItem against LBX.SelectedItem (which is the same comparison each time). You should be comparing item1 against each item2 or DDL.SelectedItem against item2 and then removing if they are the same.

Thanks for getting back to me. Is this what you mean?

For Each item In ddlProducts.Items
        For Each item1 In lbxYourProducts.Items
            If ddlProducts.SelectedItem.ToString = item1.ToString Then
                ddlProducts.Items.Remove(ddlProducts.SelectedItem)

            End If
        Next
    Next

It isn't returning any error messages, but still isn't doing anything. I've tried every variation I can think of, including

For Each item In ddlProducts.Items
            For Each item1 In lbxYourProducts.Items
                If item.ToString = item1.ToString Then
                    ddlProducts.Items.Remove(item)

                End If
            Next
        Next

And many others. Nothing that I've tried works and I'm out of ideas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.