1

I am trying to convert my old ArcGIS 9.3 (VBA) code to ArcGIS 10 using Visual Basic 2008 Express Edition. So far I have sucessfully created an pop-up window that displays the attributes of a selected feature in text boxes on the form.


I added a reference to the editor and made some adjustments to my code. My form still crashes when I try to press the button. Am I missing something or am I just referencing the attribute table in correctly?

THANKS!!!

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
 pMxDoc = My.ArcMap.Document
 Dim pMap As IMap
 pMap = pMxDoc.FocusMap
 Dim pFLayer As IFeatureSelection
 Dim Count As Integer
 'Get a reference to the editor.
 Dim editorUID As ESRI.ArcGIS.esriSystem.UID
 editorUID = New ESRI.ArcGIS.esriSystem.UID
 editorUID.Value = "esriEditor.Editor"
 Dim editor As IEditor
 editor = My.ArcMap.Application.FindExtensionByCLSID(editorUID)
 'Get FeatureLayer
 pFLayer = pMap.Layer(Count)
 Dim pFCursor As IFeatureCursor = Nothing
 pFLayer.SelectionSet.Search(Nothing, False, pFCursor)
 Dim pF As IFeature
 pF = pFCursor.NextFeature
 'Get Dataset from FeatureLayer
 Dim pEditDataset As IDataset
 pEditDataset = pFLayer
 'If not editing, start editing dataset's Workspace
 If editor.EditState <> esriEditState.esriStateEditing Then
 editor.StartEditing(pEditDataset.Workspace)
 End If
 'Initialize first (only) feature
 Dim pFeat As IFeature
 pFeat = pFLayer.NextFeature
 'Update attributes with user input
 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
 'Stop editing and save edits
 editor.StopEditing(True)
 'Refresh map
 pMxDoc.ActiveView.Refresh()
 'Close form
 Me.Close()
End Sub
asked Jan 5, 2012 at 14:43
5
  • Have you tried doing some error trapping to figure out exactly what the error is? Maybe catch the Exception and look at the Message and StackTrace? Commented Jan 6, 2012 at 15:14
  • I tried to add some catch/tries to the code, but in each area I put the catch/try, it would still crash my form Commented Jan 6, 2012 at 15:34
  • put the try before "pMxDoc = My.ArcMap.Document" and the catch, "Catch ex As Exception", after "Me.Close()". Then set a breakpoint on ex.Message or something.. Commented Jan 6, 2012 at 15:36
  • Thanks, eric, I put the try/catch where you said and a break at the message after catch and I recieved this error: "Public member 'FindExtensionByName' on type 'MxDocument' not found."...I assume this is in my code" editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")" Commented Jan 6, 2012 at 16:04
  • see my answer below Commented Jan 6, 2012 at 16:44

4 Answers 4

1

You said: "Thanks, eric, I put the try/catch where you said and a break at the message after catch and I recieved this error: "Public member 'FindExtensionByName' on type 'MxDocument' not found."...I assume this is in my code" editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")"

Try replacing it with:

editor = ArcMap.Application.FindExtensionByName("esriEditor.Editor")

answered Jan 6, 2012 at 16:44
8
  • Thanks eric...I get an error on my message box saying, "Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.IDataset'. This operation failed because the QueryInterface call on the COM component for the interface with IID " and the error takes me to" Protected Overrides Sub OnUpdate() Enabled = My.ArcMap.Application IsNot Nothing End Sub" I also get: " FeatureClass 'FeatureClass' is a type and cannot be used as an expression. " Commented Jan 6, 2012 at 17:49
  • See below for latest code Commented Jan 6, 2012 at 17:52
  • "pEditDataset = pFLayer.SelectionSet" should be "pEditDataset = pFLayer.FeatureClass" Commented Jan 6, 2012 at 18:00
  • Thanks for your patience, Eric. I think I see the problem, I don't think that I'm actually getting my FeatureLayer before the "pEditDataset=PfLayer.FeatureClass" Commented Jan 6, 2012 at 18:17
  • No prob. Dont forget to check this question as answered if it works. :D Commented Jan 6, 2012 at 18:24
1

Have you made reference to the editor, e.g.?

'Get a reference to the editor.
 Dim uid As UID
 uid = New UIDClass()
 uid.Value = "esriEditor.Editor"
 Dim editor As IEditor
 editor = CType(m_application.FindExtensionByCLSID(uid), IEditor)
answered Jan 5, 2012 at 16:17
2
  • Artwork is right. You're checking the edit state against the IEditor interface rather than the actual editor object. Commented Jan 5, 2012 at 16:33
  • Thanks for the advice..I made some changes to the code..but now my form crashes when I push the button to save the changes. I edited my code in the previous post. THANKS Commented Jan 5, 2012 at 19:37
0

C# code to find a FeatureLayer by its name, per your comment below.

public IFeatureLayer FindLayer(string aLayerName)
 {
 int i = 0;
 IMap pmap = ArcMap.Document.ActiveView.FocusMap;
 while (i <= pmap.LayerCount - 1)
 {
 if ((pmap.get_Layer(i).Name.ToUpper()) == (aLayerName.ToUpper()))
 return pmap.get_Layer(i) as IFeatureLayer;
 i++;
 }
 return null;
 }
answered Jan 6, 2012 at 18:43
0
 Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
 Try
 Dim pMxDocument As IMxDocument
 Dim pMap As IMap
 Dim pEnumLayer As IEnumLayer
 Dim pFeatureSelection As IFeatureSelection
 Dim player As ILayer
 'Get a reference to the editor.
 Dim editor As IEditor
 editor = My.ArcMap.Application.FindExtensionByName("esriEditor.Editor")
 pMxDocument = My.ArcMap.Application.Document
 pMap = pMxDocument.FocusMap
 pEnumLayer = pMap.Layers
 pEnumLayer.Reset()
 player = pEnumLayer.Next
 Do While Not player Is Nothing
 If TypeOf player Is IFeatureLayer Then
 Dim pFLayer As IFeatureLayer
 pFLayer = player
 pFeatureSelection = pFLayer
 If pFeatureSelection.SelectionSet.Count <> 0 Then
 Dim pFCursor As IFeatureCursor = Nothing
 pFeatureSelection.SelectionSet.Search(Nothing, False, pFCursor)
 Dim pFeature As IFeature
 pFeature = pFCursor.NextFeature
 If Not pFeature Is Nothing Then
 'Get Dataset from FeatureLayer
 Dim pEditDataset As IDataset
 pEditDataset = pFLayer
 '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(pFLayer)
 'Get FeatureCursor from SelectionSet
 pSelSet.Search(Nothing, True, pFCursor)
 'Initialize first (only) feature
 pFeature = pFCursor.NextFeature
 'Update attributes with user input
 pFeature.Value(pFeature.Fields.FindField("YEAR")) = txtPrjYear.Text
 pFeature.Value(pFeature.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
 pFeature.Value(pFeature.Fields.FindField("LOCATION")) = txtLocation.Text
 pFeature.Value(pFeature.Fields.FindField("AGENCY")) = txtAgency.Text
 pFeature.Value(pFeature.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
 pFeature.Value(pFeature.Fields.FindField("COST")) = txtCost.Text
 pFeature.Value(pFeature.Fields.FindField("DATEEDITED")) = txtDate.Text
 pFeature.Value(pFeature.Fields.FindField("USEREDITED")) = txtUser.Text
 'Store changes to feature
 pFeature.Store()
 End If
 End If
 End If
 player = pEnumLayer.Next
 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
answered Jan 6, 2012 at 20:25
8
  • If (pmap.Layer(i).Name.ToUpper = aLayerName.ToUpper()) Then Return pmap.Layer(i) End If Commented Jan 6, 2012 at 20:46
  • Eric, you're awesome..looks like everything is good until i update my attributes " pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text" This is my old VBA code, is it not the same using vb.net? Commented Jan 6, 2012 at 21:07
  • No, they are similar in syntax but quite a bit different. Without the error message I cant help you much Commented Jan 6, 2012 at 21:17
  • Sorry error: "System.NullReferenceException was caught Message="Object variable or With block variable not set." StackTrace: at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance) at crosoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack) at ImprovementProjectsLines.ImprovementLines.cmdUpdate_Click(Object sender, EventArgs e) in X:\PhyllisTemp\ImprovementProjectsLines\ImprovementProjectsLinesImprovementLines.vb:line 94 Commented Jan 6, 2012 at 21:26
  • pFeat is null because you assigned it to the entire featureclass instead of a discreet feature. You will need to enumerate (loop) through the featureclass to get each feature, THEN assign it a new value. Commented Jan 6, 2012 at 21:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.