Solved

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

Posted on 2012-04-12
10
1,178 Views
Last Modified: 2012-04-12
I am getting an index out of range creating this code and adding it to a dataset can anyone help
Public Shared Function GetMyADUsersInDataSet() As DataSet
        Try
            Dim dsUser As New DataSet()
            Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://OU=TARGET DIRECTORY,OU=OU,DC=COMPANY,DC=com", "USER", "PASS")
            Dim dirSearch As New DirectorySearcher()
            dirSearch.SearchRoot = dirEntry
            dirSearch.Filter = "(&(objectCategory=group)(name=TeamMembers))"
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()
            Dim dtUser As DataTable = dsUser.Tables.Add("Members")
            dsUser.Tables.Add("MEMBERS")
            dtUser.Columns.Add("member")
            If searchResults.Count > 0 Then
                For Each Result As SearchResult In searchResults
                    Dim drUser As DataRow = dtUser.NewRow()
                    drUser("member") = Result.Properties("displayName")(0) <--Here is the error
                    dtUser.Rows.Add(drUser)
                Next
            End If
            Return dsUser
        Catch ex As Exception

        End Try
    End Function

Open in new window


Pleas help
Thanks
0
Comment
Question by:r3nder
  • 4
  • 4
  • 2
10 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37839595
Try just:
drUser("member") = Result.Properties("displayName")

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37839640
Sorry. That means that Result.Properties("displayName") is returning an empty colletion, ie Result.Properties("displayName").Count = 0.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37839675
For Each Result As SearchResult In searchResults
                    if(Result.Properties("displayName").Count>0)
                             Dim drUser As DataRow = dtUser.NewRow()
                              drUser("member") = Result.Properties("displayName")(0) <--Here is the error
                              dtUser.Rows.Add(drUser)
                    End If
                Next

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 37839691
fixes that but nothing shows in the listbox except system.DirectoryServices
    Public Sub fillADUserBox()
        Try

            Dim dsUser = GetMyADUsersInDataSet()
            Me.lbxADUserDS.DataSource = dsUser.Tables("Members")
            Me.lbxADUserDS.DisplayMember = "member"
            'Me.lbxADUserDS.ValueMember = "SAMAccount"
        Catch ex As Exception
            Me.lbxADUserDS.DisplayMember = ex.Message.ToString()
        End Try

    End Sub

Open in new window

0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37839716
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 37839739
displayName is not returned as a Search Result.  Perhaps the member name is on another property.  Try the following code to verify what properties are available:
For Each Result As SearchResult In searchResults
    if(Result.Properties("displayName").Count>0)
        Dim drUser As DataRow = dtUser.NewRow()
        drUser("member") = Result.Properties("displayName")(0) <--Here is the error
        dtUser.Rows.Add(drUser)
    Else
        For Each p in Result.Properties
            Console.WriteLine("{0} = {1}", p.Key, p.Value(0)))
        Next
    End If
Next

Open in new window

0
 
LVL 6

Author Comment

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

at the same line when I add the (0) back in
0
 
LVL 6

Author Comment

by:r3nder
ID: 37839818
Here is what it looks like now
 Dim searchResults As SearchResultCollection = dirSearch.FindAll()
            Dim dtUser As DataTable = dsUser.Tables.Add("Members")
            dsUser.Tables.Add("MEMBERS")
            dtUser.Columns.Add("member")
            If searchResults.Count > 0 Then
                For Each Result As SearchResult In searchResults
                    If (Result.Properties("displayName").Count > 0) Then
                        Dim drUser As DataRow = dtUser.NewRow()
                        drUser("member") = Result.Properties("displayName")(0) '<--Here is the error
                        dtUser.Rows.Add(drUser)
                    Else
                        For Each p In Result.Properties
                            Console.WriteLine("{0} = {1}", p.Key, p.Value(0))
                        Next
                    End If
                Next
            End If
            Return dsUser

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37839911
Your dirSearch.Filter is not correct.  It's pulling the group directory entry not the group members.

It should be something like this:
dirSearch.Filter = "(&(objectClass=user)(memberOf=cn=TeamMembers,OU=Accounts and Groups))"  ' It must be the common name of your group

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37840459
Thank you wdosanjos works now :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

943 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

7 Experts available now in Live!

Get 1:1 Help Now