Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:09:45+00:00 2026-05-30T04:09:45+00:00

DirectoryEntry testAD = new DirectoryEntry(); DirectorySearcher search = new DirectorySearcher(testAD); StringBuilder add = new

  • 0
DirectoryEntry testAD = new DirectoryEntry();    
DirectorySearcher search = new DirectorySearcher(testAD);

StringBuilder add = new StringBuilder();
search.PropertiesToLoad.Add("mail");
search.Filter = "(&(objectClass=user))";

foreach (SearchResult SearchAll in search.FindAll())
{
    DirectoryEntry de = SearchAll.GetDirectoryEntry();
    add.Append(de.Properties["mail"].Value.ToString()); // error message here
}

PrefixDescription.Text = add.ToString();

I’m trying to find all emails first as a test and then all information (first name, last name, etc) and list it in a text box using a LPAR filter but I keep getting this error message when I run the app:

Object reference not set to an instance of an object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:09:46+00:00Added an answer on May 30, 2026 at 4:09 am

    Well, you’re enumerating users – but you have no guarantee that the resulting user will have an e-mail address! You need basic “programming 101” error prevention:

    .....
    foreach (SearchResult result in search.FindAll())
    {
        // this is totally unnecessary - the "SearchResult" already *contains* all
        // the properties you've defined in your "PropertiesToLoad" collection!
        // DirectoryEntry de = SearchAll.GetDirectoryEntry();
    
        if(result.Properties["mail"] != null && result.Properties["mail"].Count > 0)
        {
            add.Append(result.Properties["mail"][0].ToString());
        }
    }
    

    With this extra check, you avoid the Object reference not set.... error…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DirectoryEntry oDE = new DirectoryEntry(LDAP://DC=Test1,DC=Test2,DC=gov,DC=lk); using (DirectorySearcher ds = new DirectorySearcher(oDE)) { ds.PropertiesToLoad.Add(name); ds.PropertiesToLoad.Add(userPrincipalName);
I'm missing something here: $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter =
I use this code: DirectoryEntry objEntry; DirectorySearcher objSearchEntry; SearchResultCollection objSearchResult; string strFilter = (&(objectCategory=User));
I'm trying to use DirectoryEntry root = new DirectoryEntry(IIS://localhost/W3SVC/1/Root); foreach (DirectoryEntry de in root.Children)
I have the following code that loads a user's Active Directory DirectoryEntry object from
I'm trying to get user GUID from Active Directory. My code: DirectoryEntry entry =
I can get the OU object like.... DirectoryEntry de = new DirectoryEntry( LDAP://domain.com, DOMAIN\\Administrator,
I'm trying to programatically create a new Active Directory user setting the sAMAccountName attribute
I'm trying to create a new user record into OpenLDAP with object classes person
I'm using this query by C# in Active Directory: DirectoryEntry de = new DirectoryEntry(LDAP://

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.