How to check for null in PropertyValueCollection

M

musosdev

Hi there

I'm writing a peice of code to retrieve the ipPhone field for a given AD
user. However, some of our users don't have an ipPhone, so I need to be able
to check whether this field is null (currently, trying to retrieve a
non-existent ipPhone is throwing an error).

Sadly, I can't for the life of me work out how to add a null check on the
PropertyValueCollection.

(the code works when the ipPhone field has a value)

here's the code...

string adPath = "LDAP://thepoint.org.uk.local"; //
+ConfigurationManager.AppSettings["DefaultADServer"];

DirectoryEntry adEntry = new DirectoryEntry(adPath);
DirectorySearcher adSearch = new DirectorySearcher(adEntry);
adSearch.Filter = "(SAMAccountName=" + userSAM + ")";
adSearch.PropertiesToLoad.Add("AllProperties");
SearchResult adResult = adSearch.FindOne();

if (adResult != null)
{
adEntry = adResult.GetDirectoryEntry();
if (adEntry.Properties["ipPhone"] != null)
{
pExt = adEntry.Properties["ipPhone"][0].ToString(); //
GetPropertyValue(adEntry, "ipPhone");
}
}
else { pExt = userSAM; }

As you can see, I've tried checking adEntry.Properties["ipPhone"] != null
and also ["ipPhone"][0] for null too.

I'm sure it's obvious to someone who works with DirectoryServices all the
time, but I just can't find it!

Cheers


Dan
 
P

Peter K

musosdev said:
if (adResult != null)
{
adEntry = adResult.GetDirectoryEntry();
if (adEntry.Properties["ipPhone"] != null)
{
pExt = adEntry.Properties["ipPhone"][0].ToString(); //
GetPropertyValue(adEntry, "ipPhone");

I'm no expert - but maybe there is a
adEntry.Properties["ipPhone"]
but it has null at index [0] or has no entries at all,
so your call to ToString() or your attempt to reference index 0 fails.

What is the error you get?

Try to check the length of
adEntry.Properties["ipPhone"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top