c# active directory DirectoryEntry error if blank, Index was out of range.

Posted on 2011-05-06
Last Modified: 2013-12-16
hi all, im getting an error when an active directory attriuble is blank in my c# app

for the user im testing it on, Tel and Fax are blank (it stops at tel though with fatal exception)

System.TypeInitializationException: The type initializer for 'GlobalFunc' threw an exception. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

looking at my code below anyone tel me why and how i can ignore if its blank?

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + _Domain, null, null, AuthenticationTypes.Secure);
                DirectorySearcher searcher = new DirectorySearcher(adRoot);
                searcher.SearchScope = SearchScope.Subtree;
                searcher.ReferralChasing = ReferralChasingOption.All;
                searcher.Filter = filter;

                SearchResult result = searcher.FindOne();
                DirectoryEntry directoryEntry = result.GetDirectoryEntry();

                //string displayName = directoryEntry.Properties["displayName"][0].ToString();
                FirstName = directoryEntry.Properties["givenName"][0].ToString();
                Surname = directoryEntry.Properties["sn"][0].ToString();
                EmailAddress = directoryEntry.Properties["mail"][0].ToString();
                Company = directoryEntry.Properties["company"][0].ToString();
                Tel = directoryEntry.Properties["telephoneNumber"][0].ToString();
                Fax = directoryEntry.Properties["facsimileTelephoneNumber"][0].ToString();
                JobTitle = directoryEntry.Properties["title"][0].ToString();
                lockoutStatus = directoryEntry.InvokeGet("IsAccountLocked").ToString();

Open in new window

Question by:awilderbeast
    LVL 19

    Expert Comment

    by:Daniel Van Der Werken
    Can you test for null?

    if ( directoryEntry.Properties["company"][0]  != null )
       Tel = directoryEntry.Properties["company"][0].ToString();
    LVL 1

    Author Comment

    i dont think the problem is testing for null, because im not actually assigning tel to anything the problem is if the attribute in AD is null

    what about a try catch? that might work eh?
    LVL 1

    Accepted Solution

    try catch sorted it! :)
    LVL 1

    Author Closing Comment

    figured it out

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now