Answered by:
"Object reference not set to an instance of an object." Error when trying to display AD Results

Question
-
User678367958 posted
Hello,
i am querying successfully the Active Directory. I am searching for a User and want to show the results. This works fine for the sAMAccountName and the DisplayName, but when i want to show the SN and the GIVENNAME Properties of a User, i get the following Error:
Object reference not set to an instance of an object.
This is my Code:
Dim ds As New DirectorySearcher(searchRoot, String.Format("(&(objectClass=user)(sAMAccountName={0}))", txtSearchString.Text)) ds.PropertiesToLoad.Add("sAMAccountName") ds.PropertiesToLoad.Add("DisplayName") ds.PropertiesToLoad.Add("SN") ds.PropertiesToLoad.Add("givenName") ds.SearchScope = SearchScope.Subtree ds.Sort = New SortOption("sAMAccountName", SortDirection.Ascending) Dim sr As SearchResultCollection = ds.FindAll() If sr.Count <> 0 Then For Each i As SearchResult In sr Dim de As DirectoryEntry = i.GetDirectoryEntry response.write(de.Properties("sAMAccountName").Value.ToString) response.write(de.Properties("DisplayName").Value.ToString) response.write(de.Properties("SN").Value.ToString & ", " & de.Properties("givenName").Value.ToString) Next End If
When i use
row("DisplayName") = de.Properties("SN").Value.ToString & ", " & de.Properties("givenName").Value.ToString
i get the Errormessage. But when i comment the above line out and only use:
row("DisplayName") = de.Properties("DisplayName").Value.ToString
everything works fine and it shows me the sAMAccountName and the DisplayName. I have filled the Fields First Name and Last Name for all my Users in the AD!
Sunday, August 21, 2011 4:15 PM
Answers
-
User1508394307 posted
It can be that sn and givenName are indexed values and can be obtained with index (e.g. de.Properties("sn")(0).ToString()).
Try to use following
If (de.Properties.Contains("sn")) Then Response.Write(de.Properties("sn")(0).ToString()) End If If (de.Properties.Contains("givenName")) Then Response.Write(de.Properties("givenName")(0).ToString()) End If
This will check if property exists and return its first value.
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 21, 2011 6:12 PM
All replies
-
User1508394307 posted
It can be that sn and givenName are indexed values and can be obtained with index (e.g. de.Properties("sn")(0).ToString()).
Try to use following
If (de.Properties.Contains("sn")) Then Response.Write(de.Properties("sn")(0).ToString()) End If If (de.Properties.Contains("givenName")) Then Response.Write(de.Properties("givenName")(0).ToString()) End If
This will check if property exists and return its first value.
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 21, 2011 6:12 PM -
User678367958 posted
Thanks,
but what are indexed values?
Monday, August 22, 2011 4:31 AM -
User1508394307 posted
I must say multi-valued attributes. I think usually givenName, and surname are single-valued attributes. So, it's either a problem that they were not populated in DirectorySearcher (this can be checked using Properties.Contains), or these attributes may have multiple values (this can be checked using an index as shown above).
Monday, August 22, 2011 4:41 AM -
User678367958 posted
The solution was to check if the properties exists (contains), not to use (0) as for multivalue properties.
Monday, August 22, 2011 5:56 AM -
User-847405884 posted
THANKS A LOT !
Monday, January 30, 2012 8:05 AM