Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"index out of range ..." error when querying AD?

24 views
Skip to first unread message

E. Kwong

unread,
Oct 24, 2007, 6:17:28 PM10/24/07
to
I have a simple login form to authenticate users. A code segment looks like
this:

search.PropertiesToLoad.Add("cn");

search.PropertiesToLoad.Add("name");

search.PropertiesToLoad.Add("givenname");

search.PropertiesToLoad.Add("sn");

search.PropertiesToLoad.Add("mail");

SearchResult result = search.FindOne();

string fullname = (string)result.Properties["name"][0];

string uname = (string)result.Properties["cn"][0];

string firstname = (string)result.Properties["givenname"][0];

string lastname = (string)result.Properties["sn"][0];

string mmail = (string)result.Properties["mail"][0];

When I tested with several user accounts, some authenticated perfectly fine,
some threw the exception "Index was out of range. Must be non-negative and
less than the size of the collection. Parameter name: index" . The
exception was eventually traced to the " string mmail =
(string)result.Properties["mail"][0]; " statement. So is this because that
user has no email in AD? How best to handle this?

Am a newbie in this area. Any insight appreciate.


Alexey Smirnov

unread,
Oct 25, 2007, 2:03:55 PM10/25/07
to

Check it like this

if (searchResult.Properties.Contains("mail"))
{
...
}

0 new messages