Hi there,
I'm fairly new to VB.NET and love it but I'm not sure how to prevent a null exception from occuring. I can use the Try ... Catch to prevent the program crashing but I would prefer not to use this method. Is there a way to test for this without throwing exception I need to return a blank value for objUserInfo.Properties.Item("mail").Value for example if nothing is found.
I'm building a string from data gathered in active directory to output to a CSV file. It seems if a value is blank then an exception is thrown.
The relevant section of code is:
Dim
objUserInfo As DirectoryEntryTry
objUserInfo = GetUserInfo(objSubFolder.Name) '
' Exception occurs on the following line
s = s &
"," & objUserInfo.Properties.Item("givenName").Value & " " & _objUserInfo.Properties.Item(
"sn").Value & "," & _objUserInfo.Properties.Item(
"mail").Value Catch ex As Exception' I don't want to do it this way
s = s & ",none,none"
End TryPublic Function GetUserInfo(ByVal LoginName As String) As DirectoryEntry Dim objRootEntry As DirectoryEntry = New DirectoryEntry() Dim objADSearcher As DirectorySearcher = New DirectorySearcher(objRootEntry)
objADSearcher.Filter =
"(&(objectCategory=person)(sAMAccountName=" & LoginName & "))" Dim objResult As SearchResult = objADSearcher.FindOne() If (Not objResult Is Nothing) ThenGetUserInfo = objResult.GetDirectoryEntry()
ElseGetUserInfo =
Nothing End If End Function