deleting a feature HRESULT E_FAIL error

2444
1
07-31-2015 09:48 AM
Highlighted
New Contributor

I am trying to delete features from a feature class but i always get this error

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

at this line feature.Delete();

Here is my code:

private void DeleteRecordsFromStandardDBFeatureClass()

        {

            try

                {

                    ESRI.ArcGIS.Geodatabase.IFeatureClass destinationFeatureClass = Globals.GlobalsInstance.StandardDB.GetFeatureWorkSpaceOfDataSource().OpenFeatureClass(m_tableName);

                     ESRI.ArcGIS.Geodatabase.IFeatureCursor searchCursor = destinationFeatureClass.Search(null, false);

                    // Delete the retrieved features.

                    ESRI.ArcGIS.Geodatabase.IFeature feature = null;

                    while ((feature = searchCursor.NextFeature()) != null)

                    {

                        feature.Delete();

                    }

                }

                catch (Exception ex)

                {

                    Logger.LoggerInstance.LogError(ex.Message, DateTime.Now, "DeleteRecordsFromStandardDBFeatureClass", "MigrationFeatureClass", m_tableName);

                    throw new Exception(ex.Message);

                }

           

        }

Reply
0 Kudos
1 Reply
Highlighted
by Esri Contributor
Esri Contributor

I re-wrote the above code into an Add-in button that fetches the first featureclass in ArcMap's Table of Contents, and then creates a feature cursor, and then deletes all the features in the cursor (which ultimately deletes the features from the featureclass), but was unable to reproduce your error.

protected override void OnClick()

{

    IMap map = ArcMap.Document.FocusMap;

    IFeatureLayer featureLayer = map.get_Layer(0) as IFeatureLayer;

    IFeatureClass featureClass = featureLayer.FeatureClass;

    IFeatureCursor featureCursor = featureClass.Search(null, false);

    IFeature feature;

    while ((feature = featureCursor.NextFeature()) != null)

    {

        feature.Delete();

    }

}

It is possible that you are receiving the error due to a lock issue. Make sure that no other instance of ArcMap or ArcCatalog has a lock on that feature class when you execute your code.

Reply
0 Kudos