G
Guest
Hello,
The following code fails at Response.Write(result.Properties("mail")(0)) if
myUser does not have anything in the e-mail filed of active directory. The
message I am getting is “Object reference not set to an instance of an
objectâ€. How can I prevent from that?
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim userName As String
Dim rootEntry As New DirectoryEntry("LDAP://DC=MYDOMAIN,DC=net",
"MYDOMAIN\admin", "password")
Dim searcher As New DirectorySearcher(rootEntry)
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")
userName = "myUser"
searcher.Filter = "(&(anr=" & userName & ")(objectCategory=person))"
Dim results As SearchResultCollection
results = searcher.FindAll()
Dim result As SearchResult
For Each result In results
Response.Write(result.Properties("mail")(0))
Response.Write(result.Properties("cn")(0))
Next
End Sub
The following code fails at Response.Write(result.Properties("mail")(0)) if
myUser does not have anything in the e-mail filed of active directory. The
message I am getting is “Object reference not set to an instance of an
objectâ€. How can I prevent from that?
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim userName As String
Dim rootEntry As New DirectoryEntry("LDAP://DC=MYDOMAIN,DC=net",
"MYDOMAIN\admin", "password")
Dim searcher As New DirectorySearcher(rootEntry)
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")
userName = "myUser"
searcher.Filter = "(&(anr=" & userName & ")(objectCategory=person))"
Dim results As SearchResultCollection
results = searcher.FindAll()
Dim result As SearchResult
For Each result In results
Response.Write(result.Properties("mail")(0))
Response.Write(result.Properties("cn")(0))
Next
End Sub