• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

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
0
r3nder
Asked:
r3nder
  • 8
  • 7
1 Solution
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, 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
 
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now