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,208 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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