Hùng Nguyễn Đức Hùng Nguyễn Đức - 3 months ago 13
C# Question

Update value Ad attribute to null or empty get errror?

Here code to update mail, department, title and mobile to null or empty value of AD user.

var adSearch = new DirectorySearcher(ConnectHelper.ContxEntry);
adSearch.Filter = "samAccountName=" + "....";
var result = adSearch.FindOne();
if (result != null)
{
DirectoryEntry user = result.GetDirectoryEntry();
user.Properties["mail"].Value = null;
user.Properties["Department"].Value = null;
user.Properties["Title"].Value = null;
user.Properties["mobile"].Value = null;
user.CommitChanges();
}


Error:
"The attribute syntax specified to the directory service is invalid."

How can i set Value of some properties to null or empty???

Answer

Try using :

user.Properties["mail"].Clear() 

instead of :

user.Properties["mail"].Value = null;
Comments