Exception from HRESULT Error - Update Attributes

Discussion created by davisphyllis on Jan 9, 2012
Latest reply on May 21, 2012 by wazee
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
      Message="Exception from HRESULT: 0x80041051"
           at ESRI.ArcGIS.Geodatabase.IFeatureCursor.UpdateFeature(IFeature Object)
           at ImprovementProjectsLines.ImprovementLines.cmdUpdate_Click(Object sender,     EventArgs e)


         Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
         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
            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

                'pFeat = pFCursor.NextFeature


            'Stop editing and save edits

            'Close form

        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