Another "IFeatureWorkspace::CreateFeatureClass()" error

Discussion created by garybroyd on Mar 9, 2011
Latest reply on Mar 17, 2011 by garybroyd
Hi everybody,

We have been integrating our VB6 app with ArcMap for quite a few versions of ArcGIS now and have decided to move over to .Net due to VB6 no longer being supported (and we are going to have to do it for the next ArcGIS release anyway!)

We are encountering a number of problems while porting our code.  At the moment we have basically translated the code (which worked fine under VB6 up to ArcGIS 9.3) as is with a few tweaks here and there based on other problems we have found (like having to create classes via factories instead of using "new" keyword)

The one I'm stuck on now is one that seems to have plagued a few people but I can't seem to find any solutions which fix our problem.

You'll have to forgive me, I'm not really familiar with ArcGIS and have been given the job of getting it working with ArcGIS 10 so I'm not really up to speed on the object model of ArcGIS.

The error is bascially to do with calling CreateFeatureClass() in that it throws the error:

Error: Error HRESULT E_FAIL has been returned from a call to a COM component.

Now we have made sure we have the acquired the correct licence (it's an ArcEditor licence which we are using for in-house development purposes) as we have had other problems with this sort of thing while doing our port.

Below is some of the code for a routine which is creating a Shape File.  At the bottom of the code snippet there is a try catch which is where the error occurs.  What is odd is when running from the IDE, if I put a break point in the catch, and upon getting the exception, drag it back up and re-run the line that errored again, it then works.  Also sometimes waiting long enough before running the CreateFeatureClass() line will mean it runs ok which makes me wonder if it's some sort of timing issue.  We aren't doing anything fnacy other than starting ArcMap (via IMxDocument object), loading a document (and waiting for the "OpenDocument" event to trigger) and then creating the Shape File (a few bits inbetween but these all work fine)

Any way to the code:

Type type = Type.GetTypeFromProgID( "esriDataSourcesFile.ShapefileWorkspaceFactory" );
ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory shapefileWorkspaceFactory = Activator.CreateInstance( type ) as ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory;
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace;

featureWorkspace = shapefileWorkspaceFactory.OpenFromFile( System.IO.Path.GetDirectoryName( mapDataFilePath ), 0 ) as IFeatureWorkspace;

ESRI.ArcGIS.Geodatabase.IFields fields = new ESRI.ArcGIS.Geodatabase.Fields();
ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = fields as ESRI.ArcGIS.Geodatabase.IFieldsEdit;
IField shapeField = new ESRI.ArcGIS.Geodatabase.Field();
IFieldEdit fieldEdit = shapeField as IFieldEdit;
string filenameOnly = System.IO.Path.GetFileNameWithoutExtension(mapDataFilePath);

fieldEdit.Name_2 = "Shape";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = geometryDef as IGeometryDefEdit;

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();

fieldEdit.GeometryDef_2 = geometryDef;

fieldsEdit.AddField( shapeField );

IFeatureClass featureClass;

 featureClass = featureWorkspace.CreateFeatureClass( "Data1", fields, null, null, esriFeatureType.esriFTSimple, "Shape", "" );
catch ( Exception ex )
 Debug.Print( "Error: " + ex.Message );