I'm trying to return anattribute an identify if it has null or emtpy value. I know if i give the attribute a value it returns it find, but if it has no value it returns "object reference not set to an instance of an object"! How do i identify an empty value other than if x ==null or string x =""?

I have to return this attriute and identify if the attribute is emtpy, so i can't use a collection to output all attributes in a foreach..

any ideas would be appreciated thank you very much!

DirectoryEntry entry = new DirectoryEntry("LDAP://" + dom);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.PropertiesToLoad.Add("msExchHomeServerName");
mySearcher.Filter = "(&(objectCategory=person)(objectClass=user)(proxyaddresses=smtp:" + strsmtp + "))";
foreach (SearchResult resEnt in mySearcher.FindAll())
{
DirectoryEntry de = resEnt.GetDirectoryEntry();
string msExchHomeServerName = de.Properties["msExchHomeServerName"].Value.ToString();
if (msExchHomeServerName == null)
{
                            textBox10.Text += "Object is null" + msExchHomeServerName;
                            
}
}

You're going to want to test if the property exists before you try calling .ToString() on it and assigning it to your variable.

One method you may want to explore is the following

DirectoryEntry de = resEnt.GetDirectoryEntry();
                if (!de.Properties.Contains("msExchHomeServerName"))
                {
                    // code for when property does not exist
                }
                else
                {
                    string msExchHomeServerName = de.Properties["msExchHomeServerName"].Value.ToString();
                    // code for when it does
                }

perfect, thanks very much, i was looking but Contains must have passed me by!

Thanks again okay..

This article has been dead for over six months. Start a new discussion instead.