Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Active Directory - Null Reference Exception

Active Directory - Null Reference Exception

Scheduled Pinned Locked Moved C#
comwindows-adminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hobbsieoz
    wrote on last edited by
    #1

    Hi Im trying to retrieve user properties from active directory. It successfully gets the first required property, but after that all i get is 'NullReferenceException - Object reference not set to an instance of an object' Why is this? Here is the code: DirectoryEntry standardUser = new DirectoryEntry("LDAP://CN=" + txtUser.Text.ToString() + ",OU=Users - Standard Users,DC=mydomain,DC=com,DC=uk", "username", "password"); string sUSN = standardUser.Properties["userPrincipalName"].Value.ToString(); string nastsprofile = (standardUser.Properties["TerminalServicesProfilePath"].Value.ToString()); The bit which gets the userPrincipalName works fine. After that it all goes wrong.

    A 1 Reply Last reply
    0
    • H hobbsieoz

      Hi Im trying to retrieve user properties from active directory. It successfully gets the first required property, but after that all i get is 'NullReferenceException - Object reference not set to an instance of an object' Why is this? Here is the code: DirectoryEntry standardUser = new DirectoryEntry("LDAP://CN=" + txtUser.Text.ToString() + ",OU=Users - Standard Users,DC=mydomain,DC=com,DC=uk", "username", "password"); string sUSN = standardUser.Properties["userPrincipalName"].Value.ToString(); string nastsprofile = (standardUser.Properties["TerminalServicesProfilePath"].Value.ToString()); The bit which gets the userPrincipalName works fine. After that it all goes wrong.

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      Well, tha fact that you can get the userPrincipalName means that standardUser is fine, so the problem must be that standardUser.Properties["TerminalServicesProfilePath"].Value is null. I think you might need a lower case 'T' or something. I found this similar problem on t'internet HERE[^], where the apparent solution was to use lower case.

      My current favourite word is: Bacon!

      -SK Genius

      H 1 Reply Last reply
      0
      • A Anthony Mushrow

        Well, tha fact that you can get the userPrincipalName means that standardUser is fine, so the problem must be that standardUser.Properties["TerminalServicesProfilePath"].Value is null. I think you might need a lower case 'T' or something. I found this similar problem on t'internet HERE[^], where the apparent solution was to use lower case.

        My current favourite word is: Bacon!

        -SK Genius

        H Offline
        H Offline
        hobbsieoz
        wrote on last edited by
        #3

        Ive followed the link. And changed it to all lowercase. And replaced .value with .invokeget. Still no avail

        A 1 Reply Last reply
        0
        • H hobbsieoz

          Ive followed the link. And changed it to all lowercase. And replaced .value with .invokeget. Still no avail

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          OK, i think i have it. I'm quite sure you do need to use InvokeGet. InvokeGet will return an object, so you'll need to cast it as a string. string nastsprofile = standardUser.InvokeGet("TerminalServicesProfileName") as string; or string nastsprofile = standardUser.InvokeGet("terminalServicesProfileName") as string; or string nastsprofile = standardUser.InvokeGet("terminalservicesprofilename") as string; I'm pretty sure one of those should work.

          My current favourite word is: Bacon!

          -SK Genius

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups