VB.Net access to the Active Directory

Posted on 2008-11-20
Last Modified: 2012-05-05

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.



            _DSearch.Filter = "(&(objectClass=user) (SAMAccountName=" + UserID + "))"






            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

Question by:JJKing
    LVL 18

    Accepted Solution

    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.


    Author Closing Comment

    Thanks for that, dont know why i didnt think of that myself.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now