Solved

VB.Net listbox only getting 1000 systems

Posted on 2014-10-30
12
199 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 32

Expert Comment

by:it_saige
Comment Utility
What is the code you use to get the computers from AD?

-saige-
0
 
LVL 2

Expert Comment

by:Glen Richmond
Comment Utility
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
Comment Utility
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
Comment Utility
you have confirmed that the record set (queryResults) is returing with more than a 1000 items?
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
Comment Utility
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
Comment Utility
Basically it's putting the results in a text file.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
Here's what it said.. 1000 records see screenshot

records.png
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
Wow.. I'd never would have thought of that. Thank you.. It's now returning all my results :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

744 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

19 Experts available now in Live!

Get 1:1 Help Now