Auto Complete in VB.Net

I  am having an issue I could use some help on
I have a class (ADWrapper) and in that class I created a method (AutoPopulateUser)
It returns a list of string from active directory


     
  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


Now  this works
but i am trying to return that list in an autocomplete function on page load but I keep getting an error

Here is the code in page load
        Try
            Dim mSource As New AutoCompleteStringCollection()
            Dim UserName As New List(Of String)
          <b>  Dim suggestions As List(Of String) = New List(Of String)(New String() {ADWrapper.AutoPopulateUser(UserName)})</b>


            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


The error I am getting is
Error     1          Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'String'.
Can anyone help me  with this please - I am going crazy
where I think the error is I have bolded
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.

nepaluzCommented:
Change your bolded code to
Dim suggestions As List(Of String) = ADWrapper.AutoPopulateUser(UserName)

Open in new window

0
r3nderAuthor Commented:
Sorry that didnt work - same error
Error      1      Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'String'.
0
nepaluzCommented:
You are calling AutoPopulateUser with
Dim UserName As New List(Of String)

Open in new window

whereas the function expects a string as an argument
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

r3nderAuthor Commented:
ok I see,  this is what I changed it to

            Dim mSource As New AutoCompleteStringCollection()
            Dim UserName As String
            Dim suggestions As List(Of String) = ADWrapper.AutoPopulateUser(UserName)


            mSource.AddRange(suggestions.ToArray)

Open in new window

It still doesnt work but I have 205 records in mSource any suggestions?
0
nepaluzCommented:
You a passing an empty string as the UserName, in any case, from the original code you provided, you have commented out the search filter.

You need to give the UserName argument a value ANDALSO un-comment the code inside the function that utilises it
0
nepaluzCommented:
I probably misread your comment. What still does not work? The auto-complete or something else?
0
r3nderAuthor Commented:
the auto complete doesnt work
I did uncomment the search filter and now I return 22
I did notice the strings being applied to the mSource are like
            (8)      "CN=test03"      String
0
r3nderAuthor Commented:
I put the full search filter back and now it has an error
(&(objectCategory=user)(cn=)) Search filter is  invalid
0
nepaluzCommented:
Not sure what you mean there. Are the strings in mSource anywhere near the data you are expecting? If not, did you provide a value for UserName? Else try and type the data you see in mSource into the textbox and see if you get autocompletion on that.
0
r3nderAuthor Commented:
Here is the page load code
 Try
            Dim mSource As New AutoCompleteStringCollection()
            Dim UserName As String = txtUser.Text
            'Dim suggestions As List(Of String) = New List(Of String)(New String() {ADWrapper.AutoPopulateUser(UserName)})
            Dim suggestions As List(Of String) = ADWrapper.AutoPopulateUser(UserName)


           mSource.AddRange(suggestions.ToArray)


            With txtUser
                .AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try[/code
here is the function
[code]         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 + "))"
            'dirSearch.Filter = "(objectCategory=user)"
            '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
nepaluzCommented:
You are calling the function with a UserName argument that is nothing, that may be the reason why you are getting an error in the search filter.
I suggest you re-pipe your function to exclude any filters (since you do not have a value for UserName on page load) and therefore return all the values for your autocompletion.
0
r3nderAuthor Commented:
0
r3nderAuthor Commented:
These are some screenshots of what is going on
---new code
        Try
            Dim mSource As New AutoCompleteStringCollection()
            'Dim UserName As String
            'Dim suggestions As List(Of String) = New List(Of String)(New String() {ADWrapper.AutoPopulateUser(UserName)})
            Dim suggestions As List(Of String) = ADWrapper.AutoPopulateUser()


            mSource.AddRange(suggestions.ToArray())


            With txtUser
                'MessageBox.Show(suggestions.ToString())
                .AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
            With TextBox1
                .AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
            With TextBox2
                .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

        Public Shared Function AutoPopulateUser() 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)"
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'dirSearch.Filter = "(objectCategory=user)"
            '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

Yo can see I redid the function to take out username and  get just users
from the filter - still no auto complete
ActiveDirectoryWrapper--Running-.jpg
ActiveDirectoryWrapper--Debuggin.jpg
0
nepaluzCommented:
As far as I can tell from the graphics, the function returns a list, be that with the members prepended with "CN="

Does the autocomplete work if, for eaxample, you type CN?

I do not have web developer on this machine (won't have it till Monday!), but have tried the code using WinForms and the autocomplete works! In your case, as suggested above, try typing in a value, for example CN=Administrator, you should get the suggest appear at the latest after the A is typed.
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
r3nderAuthor Commented:
My fault this does work - I had a skin on it and it was interupting the code - as soon as I disabled the skin on the text box TADA! :) thanks for hanging in there
0
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.