Solved

VB.Net listbox only getting 1000 systems

Posted on 2014-10-30
12
207 Views
Last Modified: 2014-10-30
I'm writing code to get computers from AD. The problem I'm having is that its not getting all the computers from the directory. It seems like the ListBox only gets 1000 systems. Is there anyway to increase this?
0
Comment
Question by:AnthonySmithMCP
[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
  • 5
  • 5
  • 2
12 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40413385
What is the code you use to get the computers from AD?

-saige-
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40413403
are you using a IN clause in the query to retrieve the data? (not sure with MSSQLSever? but Oracle has a limit of 1000 items on the IN clause)
May just be a coincidence but without any of your code examples or more explanation its a good shot in the dark ;)
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40413417
Sorry.. i forgot to add the code..


       'Clears listbox
        results.Items.Clear()

        'Connect to AD Database
        Dim rootEntry As New DirectoryEntry("GC://DC=Domain,DC=org", Domain.Text + userID.Text, password.Text)

        'Set What you want to search for
        Dim searcher As New DirectorySearcher(rootEntry)
        searcher.PropertiesToLoad.Add("cn")
        searcher.PropertiesToLoad.Add("mail")

        'Set Your Search Filter
        searcher.Filter = "(objectCategory=Computer)"

        'Finds all
        Dim queryResults As SearchResultCollection
        queryResults = searcher.FindAll()

        'Puts the results into a list Box
        Dim result As SearchResult
        For Each result In queryResults
            results.Items.Add(result.Properties("cn")(0))
        Next

Open in new window

0
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!

 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40413467
you have confirmed that the record set (queryResults) is returing with more than a 1000 items?
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40413483
Not sure.... This is the code below it.. What it does is exports the code out to a text file. The text file itself only has 1000 items.. Maybe its the code and not the list box?

        If (Not System.IO.Directory.Exists("c:\CCPRemote")) Then
            System.IO.Directory.CreateDirectory("c:\CCPRemote")
        End If



        Dim W As IO.StreamWriter
        Dim i As Integer
        W = New IO.StreamWriter("c:\CCPRemote\computers.txt")

        For i = 0 To results.Items.Count - 1
            W.WriteLine(results.Items.Item(i))
        Next
        W.Close()

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40413486
Basically it's putting the results in a text file.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40413487
I agree with Glen, first let's confirm that the record set is returning more than 1000 items.

Otherwise, I am not finding anything that would limit the amount of items that you are displaying.

-saige-
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40413511
Something like:
Dim de As DirectoryEntry = New DirectoryEntry("GC://DC=Domain,DC=org", Domain.Text + userID.Text, password.Text)
Dim searcher As DirectorySearcher = New DirectorySearcher(de, "(objectCategory=Computer)")
Dim queryResults As SearchResultCollection
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")
queryResults = searcher.FindAll()

If Not queryResults Is Nothing AndAlso queryResults.Count > 0 Then
	MessageBox.Show(String.Format("Directory search returned {0}", IIf(queryResults.Count = 1, String.Format("{0} record", queryResults.Count), String.Format("{0} records", queryResults.Count))))
	results.Items.Clear()
	For Each result As SearchResult In queryResults
		results.Items.Add(result.Properties("cn")(0))
	Next
Else
	results.Items.Add("No search results found")
End If

Open in new window


-saige-
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40413537
Let me qualify my previous statement.  The listbox does have limits on what can be displayed (or the total number of items that can be added), but from the set of data you are returning I do not see anything that would challenge the limits imposed.

-saige-
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40413577
Here's what it said.. 1000 records see screenshot

records.png
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40413633
You need to set the DirectorySearcher PageSize to a non-zero value.  You could use this in conjunction with the DirectorySearcher.SizeLimit, but most related queries of this type just set the PageSize to something greater than 1000 to return the full set of results:
Dim de As DirectoryEntry = New DirectoryEntry("GC://DC=Domain,DC=org", Domain.Text + userID.Text, password.Text)
Dim searcher As DirectorySearcher = New DirectorySearcher(de, "(objectCategory=Computer)")
Dim queryResults As SearchResultCollection
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")
searcher.PageSize = 1001
queryResults = searcher.FindAll()

If Not queryResults Is Nothing AndAlso queryResults.Count > 0 Then
	MessageBox.Show(String.Format("Directory search returned {0}", IIf(queryResults.Count = 1, String.Format("{0} record", queryResults.Count), String.Format("{0} records", queryResults.Count))))
	results.Items.Clear()
	For Each result As SearchResult In queryResults
		results.Items.Add(result.Properties("cn")(0))
	Next
Else
	results.Items.Add("No search results found")
End If

Open in new window


According to the API documentation:


•when you do a paged search, the SizeLimit is ignored, and all matching results are returned as you iterate through the results returned by FindAll. Results will be retrieved from the server a page at a time. I chose the value of 1001 above, but you can use a smaller value if preferred. The tradeoff is: using a small PageSize will return each page of results faster, but will require more frequent calls to the server when iterating over a large number of results.


•by default the search isn't paged (PageSize = 0). In this case up to SizeLimit results is returned.

http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.pagesize(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit(v=vs.110).aspx

-saige-
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40413676
Wow.. I'd never would have thought of that. Thank you.. It's now returning all my results :)
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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