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,250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

630 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