Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET Combobox and SearchResultCollection

Status
Not open for further replies.

tEkHEd

IS-IT--Management
Jan 29, 2003
261
GB
Hi all...

I am having a little performance issue with the .NET combobox and a SearchResultCollection which hopefully someone can help me to alleviate..

I am basing my SearchResultCollection as an AD (DirectorySearcher) query, I then want to add the returned results into a combobox. At the moment this is very slow...

Code:
ComboBox.Items.Clear()
        'Setup the connection to the AD environment to populate our workstation combo
        Dim dEntry As New DirectoryEntry(PROV_READ & WKSOU)
        Dim dSearch As New DirectorySearcher(dEntry)

        'define the filter to use
        If Not AllRegions Then
            If DRMachines Then
                dSearch.Filter = "(&(objectclass=computer)(name=" & machineRegion & "*))"
            Else
                dSearch.Filter = "(&(objectclass=computer)(name=" & machineRegion & "*)(!name=*DR*))"
            End If
        Else
            If DRMachines Then
                dSearch.Filter = "(&(objectclass=computer))"
            Else
                dSearch.Filter = "(&(objectclass=computer)(!name=*DR*))"
            End If
        End If

        dSearch.SearchScope = SearchScope.OneLevel
        dSearch.PageSize = 1000
        dSearch.PropertiesToLoad.Add("Name")

        'Define a collection to populate
        Dim cRESULT As SearchResultCollection
        'Execute the query
        cRESULT = dSearch.FindAll
        Dim oRes As Object
        'Query the directory for each workstation and populate the combo
        For Each oRes In cRESULT
            ComboBox.Items.Add(oRes.Properties("name")(0).ToString)
        Next

The query returns about 6000 results depending on the filters being used.

The issue is then adding the items into the combo.. Can anyone help with making this faster?

Thanks in adv for any help.
 
instead of using items.add you could put you results in an arraylist and then bind the arralist to the combobox (.datasource).

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Tried that.. it just makes the enumeration even longer as I am having to double the loop.

You cannot CType or System.Convert the SearchResultCollection to an ArrayList (or dimensioned array). You simply get an InvalidCast Exception thrown. :(

I think that it has something to do with the PageSize property..

I found out that I can do a PageSize = 6 as 1 page = 1000 records, therefore 6 pages = 6000

This improves the query to a point, but is still slow.. If you don't set the PageSize property the enumeration is pretty instant, however only the first 1000 entries are returned...

I have also noticed that the method mySearcher.FindAll doesn't actually execute the query (like in ADO) rather the query is executed in the For Each statement. This seems very strange behaviour to me..

I tried with the following code to see if executing the Count method sped things up.

Code:
'Define a collection to populate
        Dim cRESULT As SearchResultCollection
        'Execute the query
        cRESULT = dSearch.FindAll
        Dim i as Integer
         'Query the directory for each workstation and populate the combo
        For i = 0 To cRESULT.Count - 1
            ComboBox.Items.Add(cRESULT(i).Properties("name")(0).ToString)
        Loop

This had no effect (unsurprisingly). I am begining to think it is a more fundamental problem with the DirectorySearcher object and the way that it pages results.. It seems like It returns 1 page, does the loop for the 1000 records and then performs the query again for the next 1000 and so on and so forth until the PageSize limit has been hit..

Sorry for my babbling and thanks for your input :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top