Intermittent failure to export feature classes

Discussion created by ollien on Mar 28, 2011
Latest reply on Mar 29, 2011 by ollien

I am having trouble writing an extraction piece of software.  The extraction needs to iterate through some 250 feature classes and extract the features that match a spatial and atrribute based query.  The software iterates through each feature class and calls the code below eventually calling the ExportFeatureClass() function.  Sometimes this works and sometimes it doesn't.  When it doesn't work it just silently fails to produce a shapefile (i.e. running the same code twice produced differing outputs where 6 shapefiles were missing from one run).  No exception is thrown.  I have tried setting various unused values to null and doing garbage collection immediately prior to the call to try and alleviate the problem but so far to no avail.

Can anyone help me please or point me as to where I should start looking..

public bool FeatureClassSelectionToShapefile(
 Logger log, bool includeEmpty, string featureClassName, string saveDirectoryPath,
 string username, string password, ArrayList whereClauses,
 ArrayList classifications, ArrayList caveats, ESRI.ArcGIS.Geometry.IGeometry areaFilter)
 int index = featureClassName.IndexOf(".");
 string exportName = featureClassName;
 if (index != -1)
  exportName = exportName.Substring(index + 1);
 bool success = false;
  IQueryFilter query = QueryUtils.SetupQuery(areaFilter);
  IWorkspace wksp = NetImporterUtils.SdeConnect.Connect(username, password, log);
  IFeatureWorkspace featwksp = wksp as IFeatureWorkspace;
  wksp = null;

  IFeatureClass cls = featwksp.OpenFeatureClass(featureClassName);
  query.WhereClause = QueryUtils.CheckQueries(cls, whereClauses, classifications, caveats);

  int numFeaturesExported = cls.FeatureCount(query);
  if (includeEmpty == true || (includeEmpty == false && numFeaturesExported > 0))
   _calculateClassificationStatistics(cls, query);
   _calculateForeignKeysForBlobs(cls, query);
   _calculateForeignKeysForTextAttributes(cls, query);

   IFeatureClassName newFcName = new FeatureClassNameClass();
   newFcName.FeatureType = cls.FeatureType;
   newFcName.ShapeType = cls.ShapeType;
   newFcName.ShapeFieldName = cls.ShapeFieldName;

   IDataset orig = cls as IDataset;
   cls = null;
   IFeatureClassName origFcName = orig.FullName as IFeatureClassName;
   IDatasetName origDsName = origFcName as IDatasetName;

   IWorkspaceName newWsName = new WorkspaceNameClass();
   newWsName.PathName = saveDirectoryPath;
   newWsName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";

   IDatasetName newDsName;
   newDsName = newFcName as IDatasetName;
   newDsName.Name = exportName;
   newDsName.WorkspaceName = newWsName;

    IExportOperation expOp = new ExportOperationClass();
    IFeatureClassName newFeatureClassName = newDsName as IFeatureClassName;
    expOp.ExportFeatureClass(origDsName, query, null, null, newFeatureClassName, 0);
    log.WriteInformation("Exported " + featureClassName + " to shapefile.");
   catch (Exception ex)
    log.WriteError("Extraction Exception: " + ex.Message);
    return false;
   success = true;
   //We are not including nulls but we have done that successfully so return true.
   success = true;
   log.WriteInformation("Feature class " + featureClassName + " excluded.");
 catch (Exception ex)
  log.WriteError("Could not export feature class " + featureClassName + " to shape file. " + ex.Message);
  success = false;
 return success;