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

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

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
0
krbnldy
Asked:
krbnldy
  • 7
  • 5
1 Solution
 
Bob LearnedCommented:
Some entries may not have all the properties that you are trying to get values for.  Which line gives you that exception?

Bob
0
 
krbnldyAuthor Commented:
it is not stopping at a line when i debug it, but just return the error after looping through and returning all the attributes for 2 users.

I do not know if i am doing something wron.
0
 
Bob LearnedCommented:
Debugging DirectoryServices stuff is kind of tricky.  One possibility would be to put in a try/catch block, and put a breakpoint on a line in the catch block to see what the exception stack trace is:

Try

Catch ex As Exception
   System.Diagnostics.Debug.WriteLine(ex.ToString())
End Try

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
krbnldyAuthor Commented:
I had the following and that is how I saw the error  "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "
but when I added the code above I still got the same thing
Catch ex As Exception
            Response.Write(ex.Message)
   End Try
0
 
Bob LearnedCommented:
What does the stack trace look like?  ex.ToString()

Bob
0
 
Bob LearnedCommented:
I am thinking that the error is coming from this line:

Dim Empname As String = result.Properties("sn")(0)

Bob
0
 
krbnldyAuthor Commented:
 WOuld this help you find where my problem is  the following is the function that is is passing the users attributes in the result.properties.

Function GetUserAttr(ByVal Empname As String) As EmpAttributes
        Dim empAttributes As New EmpAttributes
        Dim rootEntry As New DirectoryEntry("GC://OU=**,dc=**,dc=**,dc=com")
        Dim searcher As New DirectorySearcher(rootEntry)

        searcher.PropertiesToLoad.Add("cn")
        searcher.PropertiesToLoad.Add("mail")
        searcher.PropertiesToLoad.Add("memberof")
        searcher.PropertiesToLoad.Add("employeeID")
        searcher.PropertiesToLoad.Add("telephoneNumber")
        searcher.PropertiesToLoad.Add("sn")
        searcher.PropertiesToLoad.Add("displayName")
        searcher.PropertiesToLoad.Add("manager")
        searcher.PropertiesToLoad.Add("department")
        searcher.PropertiesToLoad.Add("title")

        searcher.Filter = "(&(anr=" & Empname & ")(objectCategory=person))"
        Dim results As SearchResultCollection
        results = searcher.FindAll()

        Dim result As SearchResult

        For Each result In results
            empAttributes.showname = (result.Properties("displayName")(0) & "<br />")
            empAttributes.GroupMember = (result.Properties("memberof")(0) & "<br />")
            empAttributes.emailaddress = (result.Properties("mail")(0) & "<br />")
            empAttributes.UserID = (result.Properties("cn")(0) & "<br />")
            empAttributes.EmpID = (result.Properties("employeeID")(0) & "<br />")
            empAttributes.uPhone = (result.Properties("telephoneNumber")(0) & "<br />")
            empAttributes.uDepartment = (result.Properties("department")(0) & "<br />")
            empAttributes.uManager = (result.Properties("manager")(0) & "<br />")
            empAttributes.uTitle = (result.Properties("title")(0) & "<br />")

        Next
        Return empAttributes
    End Function
0
 
Bob LearnedCommented:
It would probably help to know which property you are having problem with.  One way would be to add a try/catch block that does nothing but ignore the error, so that you can see what properties for the 'empAttributes' instance are not getting set.

Bob
0
 
krbnldyAuthor Commented:
can you tell me how i do that to ignore the error
0
 
Bob LearnedCommented:
Try

Catch ex As Exception
   ' Ignore the error
End Try

Bob
0
 
krbnldyAuthor Commented:
Thank you,  I found where I was having the problem and it was because I was not passing the groupname to the function so that it woould only return the users in that group.  ALl other user did not have attributes populated so it was failing.
0
 
Bob LearnedCommented:
Very good, grasshopper!!

Bob
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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