Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
<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.PageSize = 1000;
                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.
Posted
Updated 28-Mar-20 22:42pm

1 solution

Weird to get distinct results by just changing the search method.
I noticed that you do not use exact casing for LDAP property names. Could it be that a non-paged search performs case-insensitive searches, whereas a paged search would need proper casing? You could try
C#
usearcher.PropertiesToLoad.Add("displayName");
usearcher.PropertiesToLoad.Add("sAMAccountName");
and see whether it has any effect.

What happens on debugging? What does user.Properties contain on a paged search?
 
Share this answer
 
Comments
InsidiousBeing 29-Mar-20 3:52am    
Hey phil.o,

Thank you for responding. It didn't appear to be the name-casing, however looking into user.Properties in the debugger pointed out the issue. It was pulling contact objects as well, and it didn't like a contact that had a "\" in the displayname. Once I added (objectClass=user) to the query, BOOM! worked like a charm!

Thanks a lot, figured I was missing something simple! :)

Have a great weekend!
phil.o 29-Mar-20 11:04am    
You're welcome, have a nice weekend too!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900