/// <summary> /// Updates an existing company in AD and the database /// </summary> /// <param name="existingCompany"></param> public void Update(Company existingCompany) { OrganizationalUnits organizationalUnits = null; try { if (string.IsNullOrEmpty(existingCompany.CompanyCode)) throw new MissingFieldException("Company", "CompanyCode"); organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Find the company from SQL var company = (from c in db.Companies where !c.IsReseller where c.CompanyCode == existingCompany.CompanyCode select c).First(); if (company == null) throw new ArgumentNullException(existingCompany.CompanyCode); else { // Set the new values company.CompanyName = existingCompany.CompanyName; company.AdminName = existingCompany.AdminName; company.AdminEmail = existingCompany.AdminEmail; company.PhoneNumber = existingCompany.PhoneNumber; company.Street = existingCompany.Street; company.City = existingCompany.City; company.State = existingCompany.State; company.ZipCode = existingCompany.ZipCode; company.Country = existingCompany.Country; // Update the OU log.DebugFormat("Updating organizational unit for company {0}", existingCompany.CompanyName); organizationalUnits.Update(new OrganizationalUnit() { DistinguishedName = company.DistinguishedName, DisplayName = company.CompanyName, Description = company.Description, Street = company.Street, City = company.City, State = company.State, PostalCode = company.ZipCode, Country = company.Country }); // Save SQL changes db.SaveChanges(); log.InfoFormat("Successfully updated existing company {0}. New name if changed: {1}", existingCompany.CompanyName, company.CompanyName); } } catch (Exception ex) { log.ErrorFormat("Error updating company: {0}", ex.ToString()); throw; } finally { if (organizationalUnits != null) organizationalUnits.Dispose(); } }