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
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