Hi All,
This problem is throwing me through a loop because I don't understand what I'm doing wrong. When I try this search using PageSize=1000, I get a strange index out of bounds error on all of my properties queries. When I comment out PageSize and use Sizelimit, it works perfectly. I'm working with 12k users, so obviously Sizelimit won't work for me.
What I'm attempting to accomplish:
I have a textbox that I'm using as a search field and am trying to have that search filter the results in a listbox below it as a user types in a name.
My function:
<pre> public void FillUsers()
{
DirectoryEntry entry = new DirectoryEntry("LDAP://<mydomain>", null, null, AuthenticationTypes.Secure);
Dictionary<int, string> UserCollection= new Dictionary<int, string>();
using (DirectorySearcher usearcher = new DirectorySearcher(entry))
{
usearcher.SizeLimit = 500;
usearcher.Filter = string.Format("(&(objectCategory=Person)(mail=*))");
usearcher.PropertiesToLoad.Add("displayname");
usearcher.PropertiesToLoad.Add("samaccountname");
using (SearchResultCollection result = usearcher.FindAll())
{
int count = 0;
foreach (SearchResult user in result)
{
if (user.Properties["DisplayName"].Count >= 0 && !string.IsNullOrEmpty(user.Properties["DisplayName"][0].ToString()) && user.Properties["samaccountname"].Count >= 0 && !string.IsNullOrEmpty(user.Properties["samaccountName"][0].ToString()))
{
count++;
string u = user.Properties["DisplayName"][0].ToString() + " (" + user.Properties["samaccountname"][0].ToString() + ")";
UserCollection.Add(count, u);
}
}
}
DataTable dt = new DataTable();
dt.Columns.Add("Key", typeof(int));
dt.Columns.Add("Value");
foreach (KeyValuePair<int, string> pair in UserCollection)
{
dt.Rows.Add(pair.Key, pair.Value);
}
bs.DataSource = dt;
AddMbrs_listBox.DisplayMember = "Value";
AddMbrs_listBox.ValueMember = "Key";
AddMbrs_listBox.DataSource = bs;
}
entry.Dispose();
}
And the textchanged call:
private void Addmbr_search_TextChanged(object sender, EventArgs e)
{
if (Addmbr_search.Text != null)
{
bs.Filter = "Value LIKE '" + this.Addmbr_search.Text + "%'";
FillUsers();
}
}
Any help or assistance you could provide would be awesome... I'm very new to C# so I'm sure I have tons to learn, but I've spent the past 7 hours getting nowhere trying to resolve it on my own (much to my dismay), so I figured it was time for some guidance :)
What I have tried:
A lot of what I tried is in the code still, after doing a lot of researching online... but I did the following:
- added the
if (user.Properties["DisplayName"].Count >= 0 && !string.IsNullOrEmpty(user.Properties["DisplayName"][0].ToString()) && user.Properties["samaccountname"].Count >= 0 && !string.IsNullOrEmpty(user.Properties["samaccountName"][0].ToString()))
statement based on a lot of forum feedback stating it was returning an empty collection, which it isn't - but felt it was good to have there anyway :)
- experimented with ServerPageTimeLimit and ServerTimeLimit, thinking maybe it was timing out my connection while trying to page, didn't seem to have any effect.
- Tried a workaround where I was using the searchbox text as an input variable + * and added it into the LDAP search filter so that it was OK to be returning SizeLimit=200. This "worked", but I *really* dislike the idea of making an LDAP call everytime the textbox updates.
At that point I decided I really need to get this to work the way it seems everyone else online has it working, so I can feed it into a DataSource and filter against it.