?
Solved

VB.Net access to the Active Directory

Posted on 2008-11-20
2
Medium Priority
?
929 Views
Last Modified: 2012-05-05
Hi

I have a VB.Net program that connects to AD using DirectoryServices. I can locate a user and retrieve properties for the account. My problem is that the search retrievs only certain properties using the PropertiesToLoad functionality, one of which is the users home directory, however if the home directory is blank for a given user then i get an 'Object reference not set to an instance of an object.' error when trying to put the property.value into a variable.

I have posted the portion of the code that is failing it fails on the _HomeDirPath = Results.GetDirectoryEntry().Properties("homedirectory").Value.ToString

It appears that the property is not loaded if blank is there a way i can check for the property not being loaded.


Try
            DirectorySearcher()
            _DSearch.Filter = "(&(objectClass=user) (SAMAccountName=" + UserID + "))"
            _DSearch.PropertiesToLoad.Add("CN")
            _DSearch.PropertiesToLoad.Add("distinguishedName")
            _DSearch.PropertiesToLoad.Add("SN")
            _DSearch.PropertiesToLoad.Add("homedirectory")
 
            Dim Results As SearchResult = _DSearch.FindOne()
            _CommonName = Results.GetDirectoryEntry().Properties("CN").Value
            _DistinguishedName = Results.GetDirectoryEntry().Properties("distinguishedName").Value
            _HomeDirPath = Results.GetDirectoryEntry().Properties("homedirectory").Value.ToString
            If Not _HomeDirPath.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
                _HomeDirPath &= System.IO.Path.DirectorySeparatorChar
            End If
            _SurName = Results.GetDirectoryEntry().Properties("SN").Value
            _UserProfile = UserID
 
        Catch ex As Exception

Open in new window

0
Comment
Question by:JJKing
2 Comments
 
LVL 18

Accepted Solution

by:
exx1976 earned 500 total points
ID: 23004688
Put that in a separate try/catch block, and in the catch section set the variable to "?".  Then after you exit that try/catch, just check the value.  If it's "?" then you know the AD value was blank.

HTH,
exx
0
 

Author Closing Comment

by:JJKing
ID: 31518651
Thanks for that, dont know why i didnt think of that myself.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question