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!.

Recommended Answers

All 4 Replies

You make the assumption that the two arrays you are referencing (result.Properties[parametr1] and result.Properties[parametr2]) have entries in them. It's telling you that they don't. You need to check them first.

for (int counter = 0; counter < resultcol.Count; counter++)
                    {
                        p1 = null;
                        p2 = null;
                        result = resultcol[counter];
                        if (string.IsNullOrEmpty((String)result.Properties[parametr1][0].ToString()))
                        {
                            p1 = "NOT FOUND";
                        }
                        if (string.IsNullOrEmpty((String)result.Properties[parametr2][0].ToString()))
                        {
                            p2 = "NOT FOUND";
                        }
                        if (!string.IsNullOrEmpty((String)result.Properties[parametr1][0].ToString()))
                        {
                            p1 = (String)result.Properties[parametr1][0].ToString();
                        }
                        if (!string.IsNullOrEmpty((String)result.Properties[parametr2][0].ToString()))
                        {
                            p2 = (String)result.Properties[parametr2][0].ToString();
                        }
                        tempreport1.Add(p1);
                        tempreport2.Add(p2);
                        userList.Append(p1 + " - " + p2 + Environment.NewLine);
                    }

To avoid the problem , I made couple of ifs in case that result.Properties[parametr1] result.Properties[parametr2] are empty, but the error remains the same.
I probably still don't get the point... :(

You make the assumption that the two arrays you are referencing (result.Properties[parametr1] and result.Properties[parametr2]) have entries in them. It's telling you that they don't. You need to check them first.

In the code I have posted above, I've made some IFs should remove that error...
"IF parametr is null/empty ->add to arraylist text NOT FOUND"
I dunno why it generates errors :/

Hmmh...I have noticed that the error takes place if i choose as a parametr1 "DisplayName"...Some of objects in AD don't have displaynames..Thats the problem, and it generates an error..
I modifed the code:

if (resultcol != null)
                {
                    for (int counter = 0; counter < resultcol.Count; counter++)
                    {
                        p1 = null;
                        p2 = null;
                        result = resultcol[counter];
                        if (string.IsNullOrEmpty((String)result.Properties[parametr1][0].ToString()))
                        {
                            p1 = (String)result.Properties["sAMAccountName"][0].ToString();
                        }
                        else
                        {
                            p1 = (String)result.Properties[parametr1][0].ToString();
                        }
                        if (textBox1.Text != "null")
                        {
                            p2 = (String)result.Properties[parametr2][0].ToString();
                        }
                        else
                        {
                            p2 = "Null";
                        }
                        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";
            }
        }

And I want it to take sAMaccountName as a parametr1 if displayname is null/empty...
but it generates the same error and I think I have too look closer at:

result = resultcol[counter];

but I dunno how to make an "IF" for it...
IF parametr1 (displayname) is empty than p1 = saMAccountName

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.