This occurs when using ADSI (ActiveDs.dll) when retreiving a property value whose type (in the schema) is not the same as that specified in the method you use.
In my case I was trying to access a property which was a dn (distinguished name), however the method I was using to retrieve the value was defaulting to type ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING. This caused the following error:
Exception Information
*********************************************
Exception Type: System.Runtime.InteropServices.COMException
ErrorCode: -2147463156
Message: The directory datatype cannot be converted to/from a native DS datatype
TargetSite: System.Object GetPropertyItem(System.String, Int32)
HelpLink: NULL
Source: Active Directory
StackTrace Information
*********************************************
at ActiveDs.IADsPropertyList.GetPropertyItem(String bstrName, Int32 lnADsType)
...
When I explicitly specify the correct ADS type, in this case ADSTYPEENUM.ADSTYPE_DN_STRING, it works a treat.
Faulty code:
...
SearchResultCollection resultColl = dirSearcher.FindAll();
DirectoryEntry localDirectoryEntry = resultColl[0].GetDirectoryEntry();
localDirectoryEntry.RefreshCache(); // This should refresh the cache and avoid the error - but it doesnt
IADsPropertyList propList = (IADsPropertyList)localDirectoryEntry.NativeObject;
IADsPropertyEntry propEntry = (IADsPropertyEntry)propList.GetPropertyItem("companydn",(int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING);
Correct code:
...
SearchResultCollection resultColl = dirSearcher.FindAll();
DirectoryEntry localDirectoryEntry = resultColl[0].GetDirectoryEntry();
localDirectoryEntry.RefreshCache(); // This should refresh the cache and avoid the error - but it doesnt
IADsPropertyList propList = (IADsPropertyList)localDirectoryEntry.NativeObject;
IADsPropertyEntry propEntry = (IADsPropertyEntry)propList.GetPropertyItem("companydn",(int)ADSTYPEENUM.ADSTYPE_DN_STRING);
My environment comprised of .NET 1.1 code using the ADSI COM library (ActiveDs.dll) accessing a GDS LDAP directory.
See Brendans blog for a better explanation here.
HTH
Tim