locked
Error: Object reference not set to an instance of an object RRS feed

  • Question

  • User694411018 posted

    Can seem to find where the problem is or which objet is not referenced.

    This searches active directory finds an employeeId and then finds his managers id. 

    static void UpdateEmployeeWithManager(string empId, string mngrId) {
          try {
         
            DirectoryEntry dirEntEmp = GetUserFromEmployeeID(empId);
            DirectoryEntry dirEntManager = GetUserFromEmployeeID(mngrId);

            dirEntEmp.Properties["manager"].Value = (string)dirEntManager.Properties["distinguishedName"][0];
            dirEntEmp.CommitChanges();
          }
          catch (Exception e) {
            Console.WriteLine("Exception {0}" , e.Message);
          }
        }
        //Search userId for employee and manager in AD
        static DirectoryEntry GetUserFromEmployeeID(string empId) {
          DirectoryEntry dirEnt = new DirectoryEntry("LDAP://CN=users,DC=domain,DC=net" ,
            "a" , "b" , AuthenticationTypes.Secure);
          DirectorySearcher dirSearch = new DirectorySearcher(dirEnt);

          dirSearch.SearchScope = SearchScope.Subtree;
          dirSearch.Filter = "(&(objectClass=user)(employeeId=" + empId + "))";

          SearchResult result = dirSearch.FindOne();

          if (result == null) {
            Console.WriteLine("User with employeeID " + empId + " not found in AD");
          }
          return result.GetDirectoryEntry();
        }

    Tuesday, December 16, 2008 1:05 PM

All replies

  • User694411018 posted

     For some reason I am losing the mngrId, and this is causing the object reference error.

    I added an employeeId to to test it ; mngrId = "123456"; and it worked.

     

     

    Tuesday, December 16, 2008 2:20 PM
  • User694411018 posted

     Does anyone knows how I can maintain the reference for mngrId?

    Tuesday, December 16, 2008 3:02 PM