Directory Programming .NET

Active Directory and ADAM programming support for .NET developers
Welcome to Directory Programming .NET Sign in | Join | Help
in Search

Commit changes Error!

Last post 08-27-2008, 1:50 PM by joe. 15 replies.
Page 1 of 2 (16 items)   1 2 Next >
Sort Posts: Previous Next
  •  08-15-2008, 7:25 AM 4433

    Commit changes Error!

    I am getting an error when trying to commit the changes. I am working on a project for updating active directory with information from a database table. If I run the code without the commit changes line the code runs and it shows me all the records to be updated. But when I try the commit changes I get an error. Please look at my code I have included the error. This is just the part of the code that handles the updates. I also get an entry with the same key already exists, but I can try and work this one out.

    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);
                                dentry.Properties["telePhoneNumber"].Value = oEmployee.TelephoneNumber;
                                dentry.Properties["ipphone"].Value = oEmployee.IPPhone;
                                dentry.Properties["mobile"].Value = oEmployee.Mobile;
                                dentry.Properties["pager"].Value = oEmployee.Pager;
                                dentry.Properties["manager"].Value = oEmployee.Manager;
                                dentry.Properties["department"].Value = oEmployee.Department;
                          
                   //Error Message  
                 //"The attribute syntax specified to the directory service is invalid. (Exception from HRESULT: 0x8007200B)"
                    dentry.CommitChanges();
                                
                    dentry.Close();
                            }
                        }
                    }
                }
                de.Close();
            }
  •  08-15-2008, 12:05 PM 4436 in reply to 4433

    Re: Commit changes Error!

    That error message means:

    # as an HRESULT: Severity: FAILURE (1), Facility: 0x7, Code 0x200b
    # for hex 0x200b / decimal 8203 :
      ERROR_DS_INVALID_ATTRIBUTE_SYNTAX                             winerror.h
    # The attribute syntax specified to the directory service is
    # invalid.
    # 1 matches found for "0x8007200B"


    So, what you need to do is selectively comment out the attributes you are adding until it succeeds.  Next, figure out what the value is that you are trying to update and fix it.  My first guess is that you should concentrate on the 'manager' value as it is a DN syntax string and will likely complain if you are not exact with it.

    Ryan Dunn
    Extemporaneous Mumblings
    The .NET Developer's Guide to Directory Services Programming
  •  08-15-2008, 1:29 PM 4443 in reply to 4436

    Re: Commit changes Error!

    Yes that was right. I got some incorrect values; telephoneNumber, ipPhone and manager.
    How can I passed the manager to active directory?
    In the database is just the name, how do I convert the dame to pass it in the DN format?

    Thanks
  •  08-15-2008, 3:19 PM 4446 in reply to 4443

    Re: Commit changes Error!

    What do you have for the manager?  There are a number of ways to do this depending on the format of the data that you have for the manager in the database.  If you have the 'username' portion of 'domain\username', you can search the directory for "(sAMAccountName=username)".  That can sometimes be easiest.  You can also use DsCrackNames to move between formats (i.e. NT4 style to DN).  If you have something else, you may be forced to search on that key.


    Ryan Dunn
    Extemporaneous Mumblings
    The .NET Developer's Guide to Directory Services Programming
  •  08-18-2008, 10:41 AM 4453 in reply to 4446

    Re: Commit changes Error!

    In the database we have a collumn for Supervisor which contains the Name and LastName fo the manager.

    In active directory we are using the manager objcet from the user's class.

  •  08-18-2008, 1:11 PM 4454 in reply to 4453

    Re: Commit changes Error!

    In your case, you'll need to do a search for the user by the values you have stored in order to get their distinguishedName so you can use that value to store in the object. Make sure you are careful to ensure that you provide appropriate responses if you match 0 or >1 name with your search criteria.

    I don't think DsCrackNames will help you here if you don't have the logon name of the user so a normal LDAP search will be needed.

  •  08-21-2008, 9:13 AM 4476 in reply to 4454

    Re: Commit changes Error!

    Ok, I think I found a way to make that work.

    Now I got a problem when searching in the database and the entry to update AD is null.

     if (dentry.Properties.Contains("telephoneNumber") != null) { //this is alwys true
                                  dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
    } else {                             
     Console.WriteLine(oEmployee.TelephoneNumber + "is empty");

    }

    How can I make these condition work?.
    Thanks




  •  08-21-2008, 10:56 AM 4481 in reply to 4476

    Re: Commit changes Error!

    If you want to remove a value from an AD attribute, call the Clear method on the PropertyValueCollection.
  •  08-21-2008, 11:18 AM 4482 in reply to 4481

    Re: Commit changes Error!

    This is what I have found. If I compare the TelephoneNumber to != I get an error. But not on the other fields, the other fields gets updated except for the telephoneNumber. Any idea?

     if (oEmployee.TelephoneNumber == null)
                                {
                                  dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
                                }
                                if (oEmployee.Nextel != null)
                                {
                                  dentry.Properties["ipPhone"].Value = oEmployee.Nextel;
                                }
                                if (oEmployee.Mobile != null)
                                {
                                  dentry.Properties["mobile"].Value = oEmployee.Mobile;
                                }
                                if (oEmployee.Pager != null)
                                {
                                  dentry.Properties["pager"].Value = oEmployee.Pager;
                                }
                              if (oEmployee.HomePhone != null)
                                {
                                  dentry.Properties["homePhone"].Value = oEmployee.HomePhone;
                                }
                                //Convert Object to DN property
                                //dentry.Properties["manager"].Value = oEmployee.Manager;
                              if (oEmployee.Department != null)
                              {
                                dentry.Properties["department"].Value = oEmployee.Department;
                              }

                                dentry.CommitChanges();                           
                                dentry.Close()

  •  08-21-2008, 11:26 AM 4483 in reply to 4482

    Re: Commit changes Error!

    I'm not sure what to say about this as it appears that the TelephoneNumber property on the oEmployee object is your own custom class with a custom data source.  It doesn't appear to have anything to do with the AD object directly.

    In general, I suggest making sure that you don't attempt to set AD attribute values with null or empty string values but I'm not sure what else to tell you here.

  •  08-21-2008, 11:53 AM 4484 in reply to 4483

    Re: Commit changes Error!

    Thanks.
  •  08-21-2008, 12:47 PM 4485 in reply to 4476

    Re: Commit changes Error!

    lair:
    Ok, I think I found a way to make that work.

    Now I got a problem when searching in the database and the entry to update AD is null.

     if (dentry.Properties.Contains("telephoneNumber") != null) { //this is alwys true
                                  dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
    } else {                             
     Console.WriteLine(oEmployee.TelephoneNumber + "is empty");

    }

    How can I make these condition work?.
    Thanks


    I don't know if you noticed, the but the Contains method returns a boolean value.  Therefore, "boolean != null" will always be true.  The proper way to express what you are looking for is simply:

    if (entry.Properties.Contains("attributename")) { }


    Ryan Dunn
    Extemporaneous Mumblings
    The .NET Developer's Guide to Directory Services Programming
  •  08-25-2008, 8:09 AM 4497 in reply to 4483

    Re: Commit changes Error!

    I am still having some problems updating. I added to the code to check for null objects ,but it is not updating. I am doing what your book says and right now I just don't know what else to do. I cannot remove the if condition because It will not work only if I select an specific employeeId that has data in the database but if I select one that has nulls I get the exeption error because AD does not allows empty values to be updated.

    Any help will be very appreciated, the book has help me in some ways but right now is not.

    I include part of my class methods, please take a look and tell me were my problem might be.

    Thanks

    CODE BELLOW

    I try this and it didn't work, no update.
    if (dentry.Properties["pager"].Value != null)
       {
        dentry.Properties["pager"].Value = oEmployee.Pager;
       }
    I also try this one and no update was commited.
       if (dentry.Properties.Contains("homePhone"))
       {
        dentry.Properties["homePhone"].Value = oEmployee.HomePhone;
        }


        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;
                                }

                                //Convert Object to DN property
                                //Regex r = new   Regex(@"CN=\s*(?<manager>\w*\s*\\,\s*(?<SN>\w*)\s*,CN=\w*\s*,DC=\w*\s*");
                                //dentry.Properties["manager"].Value = oEmployee.Manager;

                                if (dentry.Properties.Contains("department"))
                                {
                                  dentry.Properties["department"].Value = oEmployee.Department;
                                }

                                dentry.CommitChanges();                           
                                dentry.Close();
                            }
  •  08-27-2008, 11:24 AM 4511 in reply to 4497

    Re: Commit changes Error!

    I'm still confused by what you are trying to do here.  Can you provide a more simple example of what works and what does not with the SQL stuff removed?  Please just show an example of what is failing when you supply different hardcoded values.

    Thanks!

  •  08-27-2008, 11:50 AM 4512 in reply to 4511

    Re: Commit changes Error!

    I have try all of the above conditions and none of them seems to work.


    if (dentry.Properties.Contains("telephoneNumber"))   {
       dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
       } //no update, this is the recommended, runs without any error but does not update the property in AD. It just ignores the second line after the condition.


    if (dentry.Properties["telephoneNumber"] !=null)   {
       dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
       } // No error, runs but does not update the property in AD 


    if (dentry.Properties["telephoneNumber"] == null)   {
       dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
       } // Exception error. If the telephoneNumber is null in the database, if is not null it will update.


    This works only if all the properties to update are not null in the database table:
    dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;


     I have read the whole chapter 6 where It explains about working with nulls and still no answer. What I am trying to do is update Active Directory users with data that comes from a table in x database.


    Any help will be very appreciated.

Page 1 of 2 (16 items)   1 2 Next >
View as RSS news feed in XML