Hi,
I got a console application that updates active directory users, the information to be updated in active directory comes from a database table. This is my problem:
right now the application works if none of the objects like (telephoneNumber, department, etc..) to be updated into active directory are not null. If an object is null I get an error. I try with an if condition, it does not give any error but does not updates the object property. My brain is very fried with this I can't think of anything else to make it work. I include part of my class that search for an user and updates ad with his information from the database.
This is what I try so far:
if (dentry.Properties["homePhone"].Value != null) {
dentry.Properties["homePhone"].Value = oEmployee.HomePhone; }
if (dentry.Properties.Contains("homePhone")) {
dentry.Properties["homePhone"].Value = oEmployee.HomePhone; }
if (restEnt.Properties.Contains("homePhone")) {
dentry.Properties["homePhone"].Value = oEmployee.HomePhone; }
if (dentry.Properties.Contains(oEmployee.HomePhone)) {
dentry.Properties["homePhone"].Value = oEmployee.HomePhone; }
if (restEnt.Properties.Contains(oEmployee.HomePhone)) {
dentry.Properties["homePhone"].Value = oEmployee.HomePhone; }
CODE:
public SortedList<string, clsEmployee> GetEmpSqlAll() {
// Collection of employee records
SortedList<string, clsEmployee> colEmployee = new SortedList<string, clsEmployee>();
SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
string cmdString = "SELECT * FROM view_database_data_to_be_inserted_in_AD";
SqlCommand myCmd = new SqlCommand(cmdString, sqlConn);
try {
SqlDataReader dataReader = myCmd.ExecuteReader();
if (dataReader != null) {
while (dataReader.Read()) {
oEmployee = new clsEmployee();
oEmployee.empId = dataReader["employeeId"].ToString();
oEmployee.Department = dataReader["Department"].ToString();
oEmployee.Manager = dataReader["Supervisor"].ToString();
oEmployee.TelephoneNumber = dataReader["WorkPhone"].ToString();
oEmployee.Nextel = dataReader["NextelDC"].ToString();
oEmployee.Mobile = dataReader["Cell"].ToString();
oEmployee.Pager = dataReader["Pager"].ToString();
oEmployee.HomePhone = dataReader["Phone"].ToString();
colEmployee.Add(oEmployee.empId, oEmployee);
}
}
else {
Console.WriteLine("Employees not found.");
}
}
catch (Exception e) {
Console.WriteLine("Error: " + e.Message);
return colEmployee;
}
sqlConn.Close();
sqlConn.Dispose();
return colEmployee;
}
public void ModifyUsers(SortedList<string, clsEmployee> colEmployee) {
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher dirsearcher = new DirectorySearcher(de);
dirsearcher.Filter = "(&(objectCategory=person)(objectClass=user))" ;
foreach (SearchResult restEnt in dirsearcher.FindAll()) {
DirectoryEntry dentry = restEnt.GetDirectoryEntry();
object prop = dentry.Properties["employeeId"];
if (prop != null) {
object empId = dentry.Properties["employeeId"].Value;
if (empId != null) {
clsEmployee emp = null;
try {
emp = colEmployee[empId.ToString()];
}
catch {
// do nothing
}
if (emp != null) {
// Update the information
Console.WriteLine(emp.empId);
if (dentry.Properties.Contains("telephoneNumber"))
{
dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
}
if (dentry.Properties.Contains("ipPhone"))
{
dentry.Properties["ipPhone"].Value = oEmployee.Nextel;
}
if (dentry.Properties["mobile"].Value != null)
{
dentry.Properties["mobile"].Value = oEmployee.Mobile;
}
if (dentry.Properties["pager"].Value != null)
{
dentry.Properties["pager"].Value = oEmployee.Pager;
}
if (dentry.Properties.Contains("homePhone"))
{
dentry.Properties["homePhone"].Value = oEmployee.HomePhone;
}
//If anyone knows how to insert a name with last name into the manager property, please reply with example
//dentry.Properties["manager"].Value = oEmployee.Manager;
if (dentry.Properties.Contains("department"))
{
dentry.Properties["department"].Value = oEmployee.Department;
}
dentry.CommitChanges();
dentry.Close();
}
Any help or code examples will be very appreciated.
Thanks