Solved

Auto Complete in VB.Net

Posted on 2012-03-23
15
659 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
listing all functions in JavaScript 19 101
Round a string to two digits 12 24
Hide Tab Page 3 18
XML & .net 5 16
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

17 Experts available now in Live!

Get 1:1 Help Now