Select to view content in your preferred language

Exception from HRESULT Error - Update Attributes

6171
9
01-09-2012 05:19 AM
PhyllisDavis
Deactivated User
I have a form that allows users to update attribute information for selected features...but I'm recieving the error: "Exception from HRESULT: 0x80041051" when it tries to update the data.  I'm using VB.net, Visual Studio Express 2008 and ArcGIS 10.

Any suggestions on where I went wrong with updating the data?

    System.Runtime.InteropServices.COMException was caught
      ErrorCode=-2147217327
      Message="Exception from HRESULT: 0x80041051"
      Source="ESRI.ArcGIS.Geodatabase"
      StackTrace:
           at ESRI.ArcGIS.Geodatabase.IFeatureCursor.UpdateFeature(IFeature Object)
           at ImprovementProjectsLines.ImprovementLines.cmdUpdate_Click(Object sender,     EventArgs e)
      InnerException:

CODE:


         Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Try
         Dim pMxDoc As IMxDocument
            pMxDoc = My.ArcMap.Document
            Dim pMap As IMap
            pMap = pMxDoc.FocusMap
            Dim pFLayer As IFeatureSelection


            'Get a reference to the editor.
            Dim editor As IEditor
            editor = My.ArcMap.Application.FindExtensionByName("esriEditor.Editor")

            'Get Crossings FeatureLayer
            Dim pFeatLyr As IFeatureLayer
            pFeatLyr = GetLayerByTOC("TransportationImprovementProjects_Lines")

            'Get Dataset from FeatureLayer
            Dim pEditDataset As IDataset
            pEditDataset = pFeatLyr

            'If not editing, start editing dataset's Workspace
            If editor.EditState <> esriEditState.esriStateEditing Then
                editor.StartEditing(pEditDataset.Workspace)
            End If

            'Get SelectionSet
            Dim pSelSet As ISelectionSet
            pSelSet = GetSelection(pFeatLyr)

            'Get FeatureCursor from SelectionSet
            Dim pFCursor As IFeatureCursor
            pSelSet.Search(Nothing, True, pFCursor)

            'Initialize first (only) feature
            Dim pFeat As IFeature
            pFeat = pFCursor.NextFeature

            'Update attributes with user input
            'Fill in Attributes of selected feature in User Form
            Do Until pFeat Is Nothing
                pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
                pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
                pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
                pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
                pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
                pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
                pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
                pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text
                pFCursor.UpdateFeature(pFeat)

                'pFeat = pFCursor.NextFeature

            Loop


            'Stop editing and save edits
            editor.StopEditing(True)

            'Close form
            Me.Close()

        Catch ex As Exception
            Windows.Forms.MessageBox.Show(ex.ToString, "error")


        End Try
    End Sub

    Public Function GetSelection(ByVal pFeatLyr As IFeatureLayer) As ISelectionSet

        'Gets selection of feature layer and returns selection set

        'Initialize the required variables
        pMxDoc = My.ArcMap.Application.Document
        Dim pFeatSel As IFeatureSelection
        pFeatSel = pFeatLyr

        GetSelection = pFeatSel.SelectionSet

    End Function

    Public Function GetLayerByTOC(ByVal lyrName As String) As IFeatureLayer

        'This function finds a feature layer based on its TOC Name

        'Initalize global variables
        pMxDoc = My.ArcMap.Application.Document
        Dim pMap As IMap
        pMap = pMxDoc.FocusMap

        Dim lyrCntr As Integer
        Dim pFeatLyr As IFeatureLayer

        For lyrCntr = 0 To pMap.LayerCount - 1

            'Ensure that the layer is valid
            If pMap.Layer(lyrCntr).Valid = True Then

                'Ensure that the layer is a feature layer
                If TypeOf pMap.Layer(lyrCntr) Is IFeatureLayer Then
                    pFeatLyr = pMap.Layer(lyrCntr)

                    If UCase(pMap.Layer(lyrCntr).Name) = UCase(lyrName) Then
                        GetLayerByTOC = pFeatLyr
                        Exit Function
                    End If
                End If
            End If
        Next lyrCntr

        Return Nothing
        'Layer not found. Show a message
        MsgBox("Layer " & lyrName & " not found !", vbCritical, "Error")

    End Function
0 Kudos
9 Replies
sapnas
by
Frequent Contributor
The error indicates that the cursor does not support update operation. Are you by any chance using read-only cursor to update feature?
0 Kudos
sapnas
by
Frequent Contributor
Use IFeatureClass.Update to populate IFeatureCursor.
0 Kudos
PhyllisDavis
Deactivated User
Use IFeatureClass.Update to populate IFeatureCursor.


Thanks for your reply..I don't use IFeatureClass, so I have pFCursor.UpdateFeature(pFeat). I believe that is the same code???
0 Kudos
AlexanderGray
Honored Contributor
Some datasets require an edit operation.  Have you tried to startoperation on the editor and stopoperation after the edit is done?  (you might want to un-comment-out the call to nextfeature too.)
0 Kudos
sapnas
by
Frequent Contributor
I'm referring to pSelSet.Search(Nothing, True, pFCursor) statement not  pFCursor.UpdateFeature(pFeat). either you  convert pSelSet it to ISelectionSet2 which has an option to instantiate update cursor or get the target featureclass from pSelSet and use IFeatureClass.Update  to instantiate updatecursor.
0 Kudos
sapnas
by
Frequent Contributor
below c# snippet might be helpful. You may have to convert it to vb.net

ISelectionSet2 pSelSet2 = pSelSet as ISelectionSet2 ;
pSelSet2.Update(null,false, out pCursor);
0 Kudos
PhyllisDavis
Deactivated User
below c# snippet might be helpful. You may have to convert it to vb.net

ISelectionSet2 pSelSet2 = pSelSet as ISelectionSet2 ;
pSelSet2.Update(null,false, out pCursor);



Thank you!! That worked great. My only problem now,  if I make changes to one feature, it saves edits and closes the form. If I try to make changes to another feature


QUOTE]System.Runtime.InteropServices.COMException was caught
  ErrorCode=-2147467259
  Message="Error HRESULT E_FAIL has been returned from a call to a COM component."
  Source="ESRI.ArcGIS.Editor"
  StackTrace:
       at ESRI.ArcGIS.Editor.IEditor.StartOperation()
       at ImprovementProjectsLines.ImprovementLines.cmdUpdate_Click(Object sender, EventArgs e) in X:\PhyllisTemp\ImprovementProjectsLines\ImprovementProjectsLines\ImprovementLines.vb:line 89
  InnerException:




Full code:
  Try


            Dim pMxDoc As IMxDocument
            pMxDoc = My.ArcMap.Document
            Dim pMap As IMap
            pMap = pMxDoc.FocusMap
            Dim pFLayer As IFeatureSelection


            'Get a reference to the editor.
            Dim editor As IEditor
            editor = My.ArcMap.Application.FindExtensionByName("esriEditor.Editor")

            'Get Crossings FeatureLayer
            Dim pFeatLyr As IFeatureLayer
            pFeatLyr = GetLayerByTOC("TransportationImprovementProjects_Lines")

            'Get Dataset from FeatureLayer
            Dim pEditDataset As IDataset
            pEditDataset = pFeatLyr

            'If not editing, start editing dataset's Workspace
            If editor.EditState <> esriEditState.esriStateEditing Then
                editor.StartEditing(pEditDataset.Workspace)
                editor.StartOperation()
            End If

            'Get SelectionSet
            Dim pSelSet As ISelectionSet
            pSelSet = GetSelection(pFeatLyr)

            'Get FeatureCursor from SelectionSet
            Dim pFCursor As IFeatureCursor = Nothing
            pSelSet.Search(Nothing, False, pFCursor)
            Dim pSelSet2 As ISelectionSet2 = CType(pSelSet, ISelectionSet2)
            pSelSet2.Update(Nothing, False, pFCursor)


            'Initialize first (only) feature
            Dim pFeat As IFeature
            pFeat = pFCursor.NextFeature

            'Update attributes with user input
            'Fill in Attributes of selected feature in User Form
            Do Until pFeat Is Nothing
                pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
                pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
                pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
                pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
                pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
                pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
                pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
                pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text
                pFCursor.UpdateFeature(pFeat)


                pFeat = pFCursor.NextFeature


            Loop


            'Stop editing and save edits
            editor.StopEditing(True)

            'Close form
            Me.Close()

        Catch ex As Exception
            Windows.Forms.MessageBox.Show(ex.ToString, "error")

0 Kudos
PhyllisDavis
Deactivated User
Actually..Just figured it out..if I comment out the Editor.StartOperation it works like a charm!!

THANKS for your help!!
0 Kudos
ZaheerHaider
Emerging Contributor
Same Problem  here when update method call error through which is show on screenshot i hope any one is figureout of my missing????
public void FindPipelineYear()
        {
          
            bool isValid = ValidateInput();
            if (isValid == false)
                return;

            // First select all junction features in given layer
            IFeatureCursor featureCursor = GetJunctionFeatures();

            UpdateStatusBar("Processing ......");
            //BeginInvoke((ThreadStart)delegate() { UpdateStatusBar("Processing ......"); });
           
            // Next update all connected pipeline features
      Error through here>>       UpdatePipelineYears(featureCursor);
        }
      public IFeatureCursor GetJunctionFeatures()
        {
        
            // First select all junction features in given layer
            IFeatureLayer junctLayer = (IFeatureLayer)this.m_map.get_Layer(this.cmbJunction.SelectedIndex);
            IFeatureClass junctFeatureClass = junctLayer.FeatureClass;
           
            IQueryFilter queryFilter = new QueryFilterClass();
            // Thi where clause selects all features in given class, also queryFilter.WhereClause = "1=1"; can be used
            queryFilter.WhereClause = "GISUTPTDS like '%Muller Tee%'";
         
          
          
            //Apply the filter to the junction feature layer
            IFeatureSelection junctSelection = (IFeatureSelection)junctLayer;
            junctSelection.Clear();
            junctSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);

            //Fire the selection changed event
            junctSelection.SelectionChanged();

            //Refresh the display...
            IActiveView activeView = (IActiveView)this.m_document.FocusMap;
            this.m_document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
         
            // Execute queryfilter
            IFeatureCursor junctCursor = junctFeatureClass.Update(queryFilter, false);

            return junctCursor;
          //  Marshal.ReleaseComObject(junctCursor);
           
        }
junctUpdateCursor.UpdateFeature(junctFeature);

                            break;

                        case "2":

                                    junctFeature.set_Value(year2Index, dateString);
                                    junctFeature.set_Value(Dia2Index, Materials);
Error through here>>    junctUpdateCursor.UpdateFeature(junctFeature);

                            break;

                        case "3":

                            junctFeature.set_Value(year3Index, dateString);
                            junctFeature.set_Value(Dia3Index, Materials);
                            junctUpdateCursor.UpdateFeature(junctFeature);

                            break;

                        case "4":

                            junctFeature.set_Value(year4Index, dateString);
                            junctFeature.set_Value(Dia4Index, Materials);
                            junctUpdateCursor.UpdateFeature(junctFeature);

                            break;

                        default:
                            break;

                    }

                    pipeFeature = pipeCursor.NextFeature();
                    index++;
                }

                // Compare dates and see if they are same or not
                bool same = true;
                String date = dates[0];
                for (int i = 1; i < pipeCount; i++)
                {
                    if (date != dates)
                    {
                        same = false;
                        break;
                    }
                }
                // Update Same field
                junctFeature.set_Value(sameIndex, same.ToString());
                junctUpdateCursor.UpdateFeature(junctFeature);


                // Get next junction feature
                junctFeature = junctUpdateCursor.NextFeature();
               }
           
            //junctFeature = junctUpdateCursor.NextFeature();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(junctUpdateCursor);
            UpdateStatusBar("Done...... " + junctCount + " Network Junction Records Updated");
           
        }
0 Kudos