Update attributes for selected (edit mode)

1336
13
Jump to solution
06-22-2012 08:26 AM
DaveCouture
New Contributor III
I'm trying to build a form that would assign a value to a field, for the selected features, during an edit session.  The user would create a new feature (in Edit mode) and the form would fill the attribute table. If the user is not in edit mode, the table won't be updated.

I'm kinda stuck there (vb.NET):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim pMxDoc As IMxDocument         Dim pLayer As ILayer         Dim pFLayer As IFeatureLayer         Dim pFeatClass As IFeatureClass         Dim pMap As IMap         Dim pCursor As ICursor         Dim pFSel As IFeatureSelection         Dim pSelSet As ISelectionSet2          pMxDoc = My.ArcMap.Document         pMap = pMxDoc.FocusMap         pLayer = pMxDoc.SelectedLayer         pFLayer = pLayer         pFeatClass = pFLayer.FeatureClass         pCursor = pFeatClass.Update(Nothing, True)         pFSel = pLayer         pSelSet = pFSel.SelectionSet         pSelSet.Update(Nothing, False, pCursor)          Do Until pFSel Is Nothing             pFSel.Value(pFSel.Fields.FindField("Source")) = "Coordinate"             pFSel = pFSel.Next         Loop  End Sub
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
I notice that you don't have the line

Inherits ESRI.ArcGIS.Desktop.AddIns.Extension

in your code. This was included in your previous code attachment

Also, you'll need to add the line

pFeature = pEnumFeature.Next

before you start the Do Loop.

View solution in original post

0 Kudos
13 Replies
FengZhang2
Occasional Contributor
Setting attribute values on features should be contained within an edit operation. Please look into the following help file for details.

-- Updating attributes of existing features --
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000001mm000000.ht...

For example,
m_Editor.StartOperation()
feature.Value(feature.Fields.FindField("LotNumberField")) = m_lotNum
feature.Value(4) = 92373
feature.Store()
m_Editor.StopOperation("Attribute update")


BTW, You can also execute updates or inserts outside an edit operation using the ITransactions interface.
0 Kudos
DaveCouture
New Contributor III
Thanks for the tips, but I still can't get it to work; ArcMap crashes all the time.

I'm using the sample code for the UseUpdateCursor, but it's not working.  I'm not sure how to get the selected feature and update the attributes of the selected.

This is the sample that I'm using: 


 
Public Sub UseUpdateCursor(ByVal featureClass As IFeatureClass)

        Dim queryFilter As IQueryFilter = New QueryFilterClass()
        queryFilter.WhereClause = "Feature = 'Test'"
        queryFilter.SubFields = "Source"

        ' Use IFeatureClass.Update to populate IFeatureCursor.
        Dim updateCursor As IFeatureCursor = featureClass.Update(Nothing, False)
        Dim typeFieldIndex As Integer = featureClass.FindField("queryFilter")

        Try
            Dim feature As IFeature = updateCursor.NextFeature()
            Do While Not feature Is Nothing
                feature.Value(typeFieldIndex) = "Coordinates"
                updateCursor.UpdateFeature(feature)
                feature = updateCursor.NextFeature()
            Loop
        Catch comExc As COMException
            ' Handle any errors that might occur on NextFeature().
        End Try

        ' If the cursor is no longer needed, release it.
        Marshal.ReleaseComObject(updateCursor)
      
    End Sub
0 Kudos
DaveCouture
New Contributor III
So, no one has a sample of how to insert values on the attribute table, for selected features in Edit mode? 

I don't want to select the Features by Query.  The user will create a new feature and by default, new features are selected in the attribute table. All I want to do, is insert values in every fields, for the selected.
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's how I do that in my editor extension Add-in. I have added a command to the edit tool context menu which allows the user to assign attributes to the selected features.

Dim pEnumFeature As ESRI.ArcGIS.Geodatabase.IEnumFeature
Dim pFeature As ESRI.ArcGIS.Geodatabase.IFeature

pEnumFeature = My.ArcMap.Editor.EditSelection
pEnumFeature.Reset()

For i As Integer = 0 To My.ArcMap.Editor.SelectionCount - 1
    pFeature = pEnumFeature.Next
' set the values for the feature

'or you could make a loop using
'  pFeature = pEnumFeature.Next
'  Do until pFeature is Nothing



0 Kudos
DaveCouture
New Contributor III
Thanks Ken.  Is there anymore to the code?  I'm trying to understand it and it doesn't like this statment: My.ArcMap.Editor.  It's underlined in blue.
0 Kudos
KenBuja
MVP Esteemed Contributor
If you're not using an add-in, then get a reference to the editor using

dim pEditor As ESRI.ArcGIS.Editor.IEditor

pEditor = m_application.FindExtensionByName("esriEditor.Editor")
pEnumFeature = pEditor.EditSelection
0 Kudos
DaveCouture
New Contributor III
Sorry, but I'm totally lost, nothing seems to work.  Are you saying that I have to use an Add-in, to build an ArcMap add-in?  Here's a screenshot of my code:

[ATTACH=CONFIG]15486[/ATTACH]
0 Kudos
KenBuja
MVP Esteemed Contributor
It looks like you don't have a reference to the Editor assembly in your project. Once you've added that, you won't need the additional lines in my previous post. Also, you should probably use the other looping structure (Do until pFeature is Nothing). I used For i As Integer = 0 To My.ArcMap.Editor.SelectionCount - 1 because I wanted to do something different with the the first feature.
[ATTACH=CONFIG]15487[/ATTACH]
0 Kudos
DaveCouture
New Contributor III
Ken, I still get the My.ArcMap.Editor in blue, after adding the Editor assembly.

[ATTACH=CONFIG]15495[/ATTACH]
0 Kudos