• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Remove items from dropdownlist based on condition

Hello, I'm using the attached code to remove items from a ddl based on not being in a list. When loop through the ddl removing items I get  to the last item and get the following error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

On this Line:

  ddlSearchCompany.Items.RemoveAt(x)

What am I missing here?
'perform platform security check on ddl platforms
        'first get the users username from his profile
        Dim userProfile As ProfileCommon = Profile.GetProfile(HttpContext.Current.User.Identity.Name)
        Dim AuthorizedPlatformList As IList = PlatformSecurityManager.GetPlatformSecurityByUsername(userProfile.Username)

        'loop through the authorized platform list and remove all platforms from the ddl that are not in the list
        For Each r As PlatformSecurity In AuthorizedPlatformList

            For x As Integer = ddlPlatforms.Items.Count - 1 To 0 Step -1
                Dim item As ListItem = ddlPlatforms.Items(x)
                If item.Value <> "Select Platform" Then
                    If item.Value <> r.PlatformName Then
                        ddlSearchCompany.Items.RemoveAt(x)
                    End If
                End If
            Next

        Next

Open in new window

0
gogetsome
Asked:
gogetsome
  • 5
1 Solution
 
käµfm³d 👽Commented:
What is the value of x when the exception is raised?
0
 
gogetsomeAuthor Commented:
I might be going at this wrong...

The ilist contains platformname which is the value in the ddl and a boolean value of true or false. I need to loop through the ddl and remove all items that are in the ilist as false.  

How can I do that?
0
 
gogetsomeAuthor Commented:
It's value is 160 and the ddlPlatforms.item.count is 161

0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
gogetsomeAuthor Commented:
The AuthorizedPlatformList looks like this:

platform1, false
platform2, flase
platform3, true

The ddl looks like this:(in both text and value)

platform1
platform2
platform2

What I need to do is remove the items from the ddl that are false in the AuthorizedPlatformList.
0
 
gogetsomeAuthor Commented:
that last platform2 in the ddl should be platform3... sorry
0
 
gogetsomeAuthor Commented:
Sorry, was wrong the value of x was not as I expected.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now