Solved

Auto Complete in VB.Net

Posted on 2012-03-23
15
671 Views
Last Modified: 2012-03-23
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
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
  • 8
  • 7
15 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757242
Change your bolded code to
Dim suggestions As List(Of String) = ADWrapper.AutoPopulateUser(UserName)

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 37757286
Sorry that didnt work - same error
Error      1      Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'String'.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757349
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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 6

Author Comment

by:r3nder
ID: 37757524
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757596
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757612
I probably misread your comment. What still does not work? The auto-complete or something else?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37757691
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
 
LVL 6

Author Comment

by:r3nder
ID: 37757728
I put the full search filter back and now it has an error
(&(objectCategory=user)(cn=)) Search filter is  invalid
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757737
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
 
LVL 6

Author Comment

by:r3nder
ID: 37757753
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 37757785
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
 
LVL 6

Author Comment

by:r3nder
ID: 37758589
0
 
LVL 6

Author Comment

by:r3nder
ID: 37758634
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
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37758875
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37759378
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

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

688 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