Problems with FindAll not finding any...

G

Guest

Hello,

I'm keping myself busy here by investigating the world of LDAP and Active
Directory. I have an application which allows users from the system to be
selected from a list box and as a result their mail address (as set in AD)
is returned. This is all fine untill I try to show the address of a user who
doesn't have one. In esscense, the DirectorySearcher.FindAll isn't finding
any.

Here's a copy of what I have:

'bind the directory entry to the root of the domain
Dim dEntry As New DirectoryEntry("LDAP://ABR-DC01")
Dim dSearch As New DirectorySearcher(dEntry)
'define the filter
dSearch.Filter = "(samAccountName=MyUserName)"
dSearch.SearchScope = SearchScope.Subtree
'define the properties to retrieve
dSearch.PropertiesToLoad.Add("mail")

'Define a collection to populate
Dim cResult As SearchResultCollection
'Excute the query
cResult = dSearch.FindAll

'query the collection and add each user name to the combo
Dim oRes As SearchResult
For Each oRes In cResult
If oRes.Properties("mail")(0) Then '<--- It errors here with no
address present
Get_Mail = "No address assigned"
Else
Get_Mail = oRes.Properties("mail")(0)
End If
Next

oRes = Nothing
cResult = Nothing
dSearch = Nothing
dEntry = Nothing

basicly, it errors when trying to do anything with oRes.Properties with an
error message of "Object reference not set to an instance of an object. "

Any idea of how i test if this collection is valid before trying to query
it? As you can see my code just isn't working.

Cheers,

<M>ike
 
G

Guest

In the end i wrapped it in a Try - End Try statement (I think I should have
done this anyway) and set the Catch statement to display a 'No Address
Found' comment. It works, but it doesn't realy answer my question.

<M>ike
 

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

Top