Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

VB.Net listbox only getting 1000 systems

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
AnthonySmithMCP
Asked:
AnthonySmithMCP
  • 5
  • 5
  • 2
1 Solution
 
it_saigeDeveloperCommented:
What is the code you use to get the computers from AD?

-saige-
0
 
Glen RichmondCustomer Reporting Programmer.Commented:
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
 
AnthonySmithMCPAuthor Commented:
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
Glen RichmondCustomer Reporting Programmer.Commented:
you have confirmed that the record set (queryResults) is returing with more than a 1000 items?
0
 
AnthonySmithMCPAuthor Commented:
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
 
AnthonySmithMCPAuthor Commented:
Basically it's putting the results in a text file.
0
 
it_saigeDeveloperCommented:
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
 
it_saigeDeveloperCommented:
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
 
it_saigeDeveloperCommented:
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
 
AnthonySmithMCPAuthor Commented:
Here's what it said.. 1000 records see screenshot

records.png
0
 
it_saigeDeveloperCommented:
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
 
AnthonySmithMCPAuthor Commented:
Wow.. I'd never would have thought of that. Thank you.. It's now returning all my results :)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now