Solved

VB.Net listbox only getting 1000 systems

Posted on 2014-10-30
12
201 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
  • 5
  • 5
  • 2
12 Comments
 
LVL 33

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
 
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 33

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 33

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 33

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

14 Experts available now in Live!

Get 1:1 Help Now