Autocomplete in VB.net using LDAP usernames

I am trying to create an auto complete textbox in VB.net using username in LDAP
I am using this in other parts of the code.

Public Shared Function GetUser(ByVal UserName As String) As DirectoryEntry
    'create an instance of the DirectoryEntry
    Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

    'create instance fo the direcory searcher
    Dim dirSearch As New DirectorySearcher(dirEntry)

    dirSearch.SearchRoot = dirEntry
    'set the search filter
    dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
    'deSearch.SearchScope = SearchScope.Subtree;

    'find the first instance
    Dim searchResults As SearchResult = dirSearch.FindOne()

    'if found then return, otherwise return Null
    If Not searchResults Is Nothing Then
        'de= new DirectoryEntry(results.Path,ADAdminUser, _
             ADAdminPassword,AuthenticationTypes.Secure);
        'if so then return the DirectoryEntry object
        Return searchResults.GetDirectoryEntry()
    Else
        Return Nothing
    End If
End Function

Open in new window

Can it be done?
LVL 6
r3nderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
You can remove the filter to get all users back and then use that as autocomplete source.
0
r3nderAuthor Commented:
I removed the filter and changed the function name

In the page load
       
 Try

            ADWrapper.AutoPopulateUser(UserName)
            txtUser.AutoCompleteCustomSource.Clear()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window


How whould I add it to a loop to start
0
CodeCruiserCommented:
You changed it to do a FindAll instead of FindOne and used a searchresultcollection class right?

http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

r3nderAuthor Commented:
This is what I did
 Public Shared Function AutoPopulateUser(ByVal UserName As String) As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            If Not searchResults Is Nothing Then
                'de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
                'if so then return the DirectoryEntry object
                Return searchResults.GetEnumerator()
            Else
                Return Nothing
            End If
        End Function

Open in new window

now how do I loop through it to get results?
Thank you for the help
0
r3nderAuthor Commented:
Do I  use the on text changed for the text box and call the AutoPopulateUser and use the searchresults (Dim serachresults)in the pageload
0
CodeCruiserCommented:
Use the ForEach on SearchResults object to populate a list of string.

Call the function to populate the list of string on form load. Then set the textbox properties and it will take care of using the list to make suggestions or append matching names depending on your chosen mode.
0
r3nderAuthor Commented:
so.......not getting it?
Dim results As List(Of String) = New List(Of String)
	vals.Add("???")
	vals.Add("???")
	vals.Add("???")

        Try

            ADWrapper.AutoPopulateUser(Nothing)
            Dim results = ADWrapper.AutoPopulateUser(searchResults)
            txtUser.AutoCompleteCustomSource.Clear()
            For Each s In results
                txtUser.AutoCompleteCustomSource.Add(s)

            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window

0
CodeCruiserCommented:
Modify your function so that it returns a list(of string). Then in function

change

            If Not searchResults Is Nothing Then
                'de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
                'if so then return the DirectoryEntry object
                Return searchResults.GetEnumerator()
            Else
                Return Nothing
            End If

Open in new window



to


    Dim names As New List(of String)
            If Not searchResults Is Nothing Then
               For Each result in SearchResults
                    names.Add(result.GetDirectoryEntry().Name)
               Next
            End If
     Return Names

Open in new window


Once you have this list of names, we will move to next part.
0
r3nderAuthor Commented:
Done please see code I also had to Dim the result

       
 Public Shared Function AutoPopulateUser(ByVal UserName As String) As List(Of String)
            'Public Shared Function AutoPopulateUser() As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            Dim result
            Dim names As New List(Of String)
            If Not searchResults Is Nothing Then
                For Each result In searchResults
                    names.Add(result.GetDirectoryEntry().Name)
                Next
            End If
            Return names

        End Function

Open in new window

0
CodeCruiserCommented:
So now you successfully get a list of names?

Set the list of names as AutoCompleteSource property of the textbox. See example in below link

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletesource.aspx
0
r3nderAuthor Commented:
Does this look correct? - I dont know if it takes a  long time to query or if its not working
but its not doing autocomplete

       
Dim mSource As New AutoCompleteStringCollection()
        mSource.Add(ADWrapper.AutoPopulateUser(txtUser.Text).ToString())
        With txtUser
            txtUser.AutoCompleteCustomSource = mSource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .Visible = True
        End With

Open in new window

0
r3nderAuthor Commented:
Here is my updated code can anyone tell me why ADentry in Dim Ds As New List(of ADEntry) undefined?
        Try
            Dim ADEntry As New AutoCompleteStringCollection()
            Dim Ds As New List(Of ADEntry)()
            Ds = New DirectoryEntry("MYSERVER", "MYUSERNAME", "MYPASSWORD")
            Dim dt As New DataTable()
            dt.Columns.Add("Name", GetType(String))
            dt.Columns.Add("Email", GetType(String))
            For Each adentry As ADEntry In Ds
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 0 To adentry.Information.Count - 1
                    If adentry.Information(i).Key = "mail" Then
                        dr("Email") = adentry.Information(i).Value
                    End If
                    If adentry.Information(i).Key = "displayname" Then
                        dr("Name") = adentry.Information(i).Value
                    End If
                Next
                If Not dr("Email").ToString().Equals("") Then
                    dt.Rows.Add(dr)
                End If
            Next

Open in new window

0
r3nderAuthor Commented:
Could anyone help with this - I have my function in my class it returns a List(Of string)
I am trying to auto populate it in a textbox I have - how do I assign the List(of string) from that function
My Auto complete code is
        Try
            Dim mSource As New AutoCompleteStringCollection()
            mSource.AddRange(ADWrapper.AutoPopulateUser(txtUser.Text))


            With txtUser
                txtUser.AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window


my function in the class is (and this works)
       Public Shared Function AutoPopulateUser(ByVal UserName As String) As List(Of String)
            'Public Shared Function AutoPopulateUser() As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            Dim result
            Dim names As New List(Of String)
            If Not searchResults Is Nothing Then
                For Each result In searchResults
                    names.Add(result.GetDirectoryEntry().Name)
                Next
            End If
            Return names

Open in new window


can someone PLEASE HELP
0
r3nderAuthor Commented:
Am I close? it keeps telling me UserName is undefined
       
 Try
            Dim mSource As New AutoCompleteStringCollection()
            Dim UserName As New List(Of String)
            Dim suggestions As List(Of String) = New List(Of String)(New String() {ADWrapper.AutoPopulateUser(Username)})


            mSource.AddRange(suggestions.ToArray)


            With txtUser
                txtUser.AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window

Any help please?  would be greatly appreciatred
0
r3nderAuthor Commented:
i found the problem with UserName now it is telling me
Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'String'.
Any help is always appreciated
0
r3nderAuthor Commented:
I found it - it works now
0
CodeCruiserCommented:
I helped you half the way didn't i?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.