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,148 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
Comment Utility
Try just:
drUser("member") = Result.Properties("displayName")

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you wdosanjos works now :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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