I am trying to get the subtypes of a feature class that has subtypes but i get this error
"
Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.ISubtypes'. This
operation failed because the QueryInterface call on the COM component for the interface with IID '{AEF78514-848F-11D2-
AABA-00C04FA37B82}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))
"
at this line :ESRI.ArcGIS.Geodatabase.ISubtypes subtypes = (ESRI.ArcGIS.Geodatabase.ISubtypes)sourceFeatureClass;
Here is the code:
protected override List<String> GetDistinctGroupFieldValues(MigrationFeatureClassSource source)
{
if (source.SourceName == "Mekka")
{
try
{
List<String> distinctValues = null;
ESRI.ArcGIS.Geodatabase.IFeatureClass sourceFeatureClass =
source.DataSource.GetFeatureWorkSpaceOfDataSource().OpenFeatureClass(source.SourceTableName);
ESRI.ArcGIS.Geodatabase.ISubtypes subtypes = (ESRI.ArcGIS.Geodatabase.ISubtypes)sourceFeatureClass;
ESRI.ArcGIS.Geodatabase.IEnumSubtype enumSubtype;
int subtypeCode;
string subtypeName;
if (subtypes.HasSubtype)
{
distinctValues = new List<String>();
enumSubtype = subtypes.Subtypes;
subtypeName = enumSubtype.Next(out subtypeCode);
while (subtypeName != null)
{
subtypeName = enumSubtype.Next(out subtypeCode);
if (subtypeName.Contains("???"))
{
distinctValues.Add(subtypeCode.ToString());
}
}
}
return distinctValues ;
}
catch (Exception ex)
{
Logger.LoggerInstance.LogError(ex.Message, DateTime.Now, "GetDistinctGroupFieldValues",
"MigrationFeatureClass", m_tableName);
throw new Exception(ex.Message);
}
}
else
{
return base.GetDistinctGroupFieldValues(source);
}
}
This exception will occur if the object returned by the following method is in fact not an IFeatureClass residing in a geodatabase:
IFeatureClass sourceFeatureClass = source.DataSource.GetFeatureWorkSpaceOfDataSource().OpenFeatureClass(source.SourceTableName);
I am thinking that your source.SourceTableName above is in fact just a normal excel CSV file (table) or a shapefile, and not really a table/feature class coming from a real geodatabase. Remember, Subtypes can only be created on a feature class (or table) residing in a geodatabase. This means, it has got to be a feature class (or table) residing in (a) a Personal Geodatabase, (b) File Geodatabase, (c) ArcSDE Geodatabase (multiuser geodatabase). A shapefile would not work. An Excel CSV table would not work. The below line of code would fail (you cannot cast a shapefile or CSV excel table to an ISubtypes Object) because subtypes are only available within the geodatabase container.
ISubtypes subtypes = (ISubtypes)sourceFeatureClass;
Try replacing source feature class with a known feature class residing in a file geodatabase or an ArcSDE geodatabase, and see if you still get the error.
Documentation on Subtypes can be found at the below URLs:
A Quick tour of Subtypes
Creating Subtypes (using ArcCatalog)
http://resources.arcgis.com/en/help/main/10.2/index.html#/Creating_subtypes/005r00000003000000/
Creating Subtypes (using ArcObjects)