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,234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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