Geeks With Blogs

Tim Huffam Dotting the I and crossing the T of I.T.

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

Posted on Wednesday, November 9, 2005 1:17 PM C# .NET , OS/Networking/Misc Apps , ASP.NET , LDAP, AD, ADSI, DS, GDS | Back to top


Comments on this post: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.

# re: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Requesting Gravatar...
I just read your post. I had a similar issue that was caused by ADSI's inability to read the schema of a 3rd party V2 LDAP server. Instead of using COM interop with ADSI, I upgraded to .NET 2.0. The System.DirectoryServices.Protocols namespace contains a new set of classes that give a lower level of control when accessing LDAP servers. This solved my problem, as the classes in the System.DirectoryServices.Protocols namespace reference W32LDAP.dll and not the ADSI COM layer. The System.DirectoryServices.Protocols classes worked like a charm.
Left by Kory on Nov 10, 2005 7:10 AM

# re: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Requesting Gravatar...
Thanks Kory
Yes, apparently later versions of .Net and ADSI resolve the problem.
The version of ADSI I am using is 2.0 - and version 2.5 addresses this very issue.
Left by Tim on Nov 10, 2005 7:20 AM

# re: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Requesting Gravatar...
Thanks heaps for the info. I have spent a couple of weeks trying to cure a similar problem with the same error message.

My situation was:
Web App developed in vb.net 1.1 running on a member server which was retrieving data from an AD server. The web app worked perfectly when run from the AD server but failed on certain properties (sAMAccountName and Department to name a few) when run from the member server.

Here's my vb.net function that fixes it in case anybody wants to see it.

Imports System.DirectoryServices
Imports ActiveDs
....
GetProperty = String.Empty
Try
If oDE.Properties.Contains("sAMAccountName") Then
GetProperty = oDE.Properties("sAMAccountName")(0).ToString()
End If
Catch
'Catches the ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Dim propList As IADsPropertyList = oDE.NativeObject
Dim propEntry As IADsPropertyEntry = propList.GetPropertyItem("sAMAccountName", ADSTYPEENUM.ADSTYPE_PRINTABLE_STRING)
Try
GetProperty = propEntry.Values(0).PrintableString
Catch
End Try
End Try

Left by Randy on Nov 18, 2005 1:43 AM

# re: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Requesting Gravatar...
Hello all, How to I check the version of my ADSI? What I did is checking the dll file version in windows/system32 directory. Am I right, but my version is 5 which does not make sense with 2.0 or 2.5.
Left by Yap Sok Chuan on Jun 12, 2006 1:21 AM

# re: ADSI error: The directory datatype cannot be converted to/from a native DS datatype. When retrieving DS property value.
Requesting Gravatar...
Thank you very much. This solve my problem.
Left by Yap Sok Chuan on Jun 12, 2006 6:07 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net