troubleshooting Question

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

Avatar of krbnldy
krbnldy asked on
.NET ProgrammingASP.NETEditors IDEs
12 Comments1 Solution2810 ViewsLast Modified:
I have the following code that is retrieving user attributes from active directory bades on a usergroup name.  I am getting the error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"  after  some atrributes are returned even though there are several users in the group .  IN this example I got 2 different users then the error was returned.  What would be causing this to error out after returning some data.

 Dim empattributes2 As New EmpAttributes

            Dim groupname As String = "admins"
            Dim rootEntry As New DirectoryEntry("GC://**=**,dc=cits,***=***,dc=com")
            Dim searcher As New DirectorySearcher(rootEntry)

            searcher.PropertiesToLoad.Add("sn")
            searcher.Filter = "(&(objectCategory=user)(memberOf=CN=" & groupname & ",OU=**,OU=**,DC=***,DC=***,DC=com))"

            Dim results As SearchResultCollection
            results = searcher.FindAll()

            Dim result As SearchResult

            For Each result In results
                             Dim Empname As String = (result.Properties("sn")(0))
                empattributes2 = GetUserAttr(Empname)
                lblMsg.Text = empattributes2.EmpID & " " & empattributes2.showname & " " & empattributes2.emailaddress & " " & empattributes2.GroupMember & " " & empattributes2.uDepartment & " " & empattributes2.uManager & " " & empattributes2.uPhone & " " & empattributes2.UserID & " " & empattributes2.uTitle

            Next
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros