Remove items from dropdownlist based on condition

Posted on 2011-10-06
Last Modified: 2012-05-12
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:


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
                    End If
                End If


Open in new window

Question by:gogetsome
    LVL 74

    Accepted Solution

    What is the value of x when the exception is raised?

    Author Comment

    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?

    Author Comment

    It's value is 160 and the ddlPlatforms.item.count is 161


    Author Comment

    The AuthorizedPlatformList looks like this:

    platform1, false
    platform2, flase
    platform3, true

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


    What I need to do is remove the items from the ddl that are false in the AuthorizedPlatformList.

    Author Comment

    that last platform2 in the ddl should be platform3... sorry

    Author Closing Comment

    Sorry, was wrong the value of x was not as I expected.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now