Hi all,
I am trying to get a user's email address based on a LDAP query to AD, it work in local domain.
When I try to upload to other domain, it show the
following error.
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and
where it originated in the code.
Exception Details:
System.NullReferenceException: Object reference not set to an instance of an
object.
Source Error:
Line 291:'exit function Line 292: Line 293: Return CStr(myResultPropValueColl.Item(0)) Line 294: Line 295: ' Catch ex As System.Exception |
I am using .net 1.1. Here is the code I'm using for the retrieval:
Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
Dim sEmail As String =
GetUserInfo(UserAccount, "mail")
exit sub
Public
Function GetUserInfo(ByVal inSAM
As
String, ByVal inType As String) As String
Dim sPath As String = "LDAP://yourdomainpath.com/DC=yourdomainpath,DC=com"
Dim SamAccount As String = Right(inSAM,
Len(inSAM) - InStr(inSAM, "\"))
Dim myDirectory
As
New DirectoryEntry(sPath, "Enterprise Admin", "Password")
Dim
mySearcher As New DirectorySearcher(myDirectory)
Dim mySearchResultColl As
SearchResultCollection
Dim mySearchResult
As SearchResult
Dim myResultPropColl
As ResultPropertyCollection
Dim
myResultPropValueColl As
ResultPropertyValueCollection
mySearcher.Filter =
("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
mySearchResultColl = mySearcher.FindAll()
Select Case
mySearchResultColl.Count
Case
0
Return "Null"
Exit
Function
Case Is > 1
Return "Null"
Exit Function
End Select
mySearchResult = mySearchResultColl.Item(0)
myResultPropColl = mySearchResult.Properties
myResultPropValueColl =
myResultPropColl.Item(inType)
Return CStr(myResultPropValueColl.Item(0))End Function
I am appreciate any help can provide.
Best Regards,
YuKi