Error on Updating Feature's Value

457
1
11-22-2017 06:47 AM
ShaningYu
Frequent Contributor

I have the code below for testing whether a feature class is editable or not.  In this piece of code, I use

    bool b_Delete = false;              // true for Delete, false for Update

to control if the test is for Delete or for Update scenario.  The results that it works well for Delete Scenario, but not successful for the Update Scenario.  The error received is

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

on the line:

   featureCursor.UpdateFeature(feature);

What's wrong in my code?  Thanks if you can help.

  public  bool isFeatureEditable(string fcName, string versionName, ESRI.ArcGIS.Geodatabase.IWorkspace ws,
            ESRI.ArcGIS.ArcMapUI.IMxDocument mxDoc)   {

            ESRI.ArcGIS.Geodatabase.IVersionedWorkspace verWorkspace = (ESRI.ArcGIS.Geodatabase.IVersionedWorkspace)ws;
            ESRI.ArcGIS.Geodatabase.IVersion editorVersion = (ESRI.ArcGIS.Geodatabase.IVersion)verWorkspace.FindVersion(versionName);
            ESRI.ArcGIS.Carto.IFeatureLayer fLyr = LayerHelper.GetFeaturelayerFromTocByName(fcName, mxDoc);
            if (fLyr != null) {
                ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = (ESRI.ArcGIS.Geodatabase.IWorkspaceEdit)ws;
                ESRI.ArcGIS.Geodatabase.IMultiuserWorkspaceEdit muWorkspaceEdit = (ESRI.ArcGIS.Geodatabase.IMultiuserWorkspaceEdit)editorVersion;
                muWorkspaceEdit.StartMultiuserEditing(ESRI.ArcGIS.Geodatabase.esriMultiuserEditSessionMode.esriMESMVersioned);
                if (workspaceEdit.IsBeingEdited())   {
                    try  {
                        // **** Updating ****
                        workspaceEdit.StartEditing(true);
                        workspaceEdit.StartEditOperation();

                        ESRI.ArcGIS.Geodatabase.IFeature feature = fLyr.FeatureClass.GetFeature(1);
                        bool b_Delete = false;              // true for Delete, false for Update
                        if (b_Delete)
                            feature.Delete();
                        else {
                            ESRI.ArcGIS.Geodatabase.IFields fields = fLyr.FeatureClass.Fields;
                            int idx = -1;
                            string sFieldName = "";
                            for (int i = 0; i < fields.FieldCount; i++)  {
                                ESRI.ArcGIS.Geodatabase.IField field = fields.Field;
                                if (field.Editable && field.Type.ToString() == "esriFieldTypeString")  {
                                    idx = i;
                                    sFieldName = field.Name;
                                    break;
                                }
                            }

                            string sValue = feature.get_Value(idx).ToString();
                            feature.set_Value(idx, sValue + " New");

                            // Create a new query filter.
                            ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                            queryFilter.WhereClause = "OBJECTID = " + feature.get_Value(feature.Fields.FindField("OBJECTID"));
                            // Get an update cursor constrained by the query filter.
                            ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = fLyr.FeatureClass.Update(queryFilter, false);
                            featureCursor.UpdateFeature(feature);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
                        }

                        workspaceEdit.StopEditOperation();

                        bool hasEdits = false;
                        workspaceEdit.HasEdits(ref hasEdits);
                        if (hasEdits)
                            workspaceEdit.UndoEditOperation();

                        workspaceEdit.StopEditing(true);

                        return true;
                    }  catch (Exception ex)  {
                        string sMsg = ex.Message;
                    }
                }
            }
            return false;
        }

Tags (1)
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

If you want someone to reply please set the language syntax highlighting for the code to it's language as it is difficult for anyone to read.

0 Kudos