I have a serious problem, I read 100+ articles all over the web about tis exemption, and I cannot solve my problem...
String query(string groupuser, string parametr1, string parametr2)
{
try
{
string connectionPrefix = "LDAP://" + domain;
DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
DirectorySearcher search = new DirectorySearcher(entry);
if (groupuser == "group" && textBox1.Text == "null")
{
search.Filter = "(&(objectClass=group)(|(!" + parametr2 + "=*)))";
}
if (groupuser == "user" && textBox1.Text == "null")
{
search.Filter = "(&(objectClass=user)(|(!" + parametr2 + "=*)))";
}
if (groupuser == "group" && textBox1.Text != "null")
{
search.Filter = "(&(objectClass=group)(|(" + parametr2 +"=" + textBox1.Text + ")))";
}
if (groupuser == "user" && textBox1.Text != "null")
{
search.Filter = "(&(objectClass=user)(|(" + parametr2 + "=" + textBox1.Text + ")))";
}
search.PropertiesToLoad.Add(parametr2);
search.PropertiesToLoad.Add(parametr1);
StringBuilder userList = new StringBuilder();
SearchResult result;
SearchResultCollection resultcol = search.FindAll();
if (resultcol != null)
{
for (int counter = 0; counter < resultcol.Count; counter++)
{
result = resultcol[counter];
string p1 = (String)result.Properties[parametr1][0].ToString();
string p2 = (String)result.Properties[parametr2][0].ToString();
tempreport1.Add(p1);
tempreport2.Add(p2);
userList.Append(p1 + " - " + p2 + Environment.NewLine);
}
}
return userList.ToString();
}
catch (Exception E)
{
return "ERROR: No string found\r\n" + E + "\r\n";
}
}
My code list user accounts/groups (in Active Directory) with a description provided by the user.
Function works in 99%, but when i type "ow: M*"..To show all users with description which begins with "ow: M", I receive an error:
Index was out of range. Must be non-negative and less than the size of collection.
I do not understand....
resultcol is my collection.
resultcol.Count is the end size for loop. So why this error occurs?
and why only in some specific situations?
I have noticed that these 2 lines generate error:
string p1 = (String)result.Properties[parametr1][0].ToString();
string p2 = (String)result.Properties[parametr2][0].ToString();
and as I said before...Only with some specific situations..I have no idea why it generates errors..
Please help!.