Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have developed a command button for ArcGIS 9.2 (Visual Studio 2005) that copies the selected features of a featureclass to a shapefile. It then examines the attribute table of the shapefile (via a loop) for three specific field names. If a field name isn't found, a new column (type text) is created with that name, and the user is presented a Windows form that I created that offers three options for populating that field:

  • Copy an existing field: The user selects an existing field from a drop-down comboBox
  • Enter a value: The user enters a value to be used to populate the field
  • Leave blank: A space is entered in the field

All of these are accomplished via a call to CalculateField_management within the loop.

If the user chooses the same option for the first and second time through the loop, it will work for the third iteration. However, if different options are chosen for the first and second it will fail on the third iteration, and I get this message in the debugger:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in SDtools.dll System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.

The error points to the line with the CalculateField command in the code.

Does anyone know why this is occurring? Will post code if necessary.

-code-

Dim pGP As IGeoProcessor = New GeoProcessor
Dim pParams As IVariantArray = New VarArrayClass
pGP.OverwriteOutput = True

Dim Form As frmAddFieldAttribute = New frmAddFieldAttribute
Dim pTable As ITable = CType(pMxDoc.FocusMap.Layer(0), IAttributeTable).AttributeTable
SearchFields(pTable.Fields, Form.cbCopyAttribute) 'Adds existing fields to the comboBox (option 1)

Dim fieldList As New System.Collections.ArrayList
fieldList.Add("TRACTNBR")
fieldList.Add("FARMNBR")
fieldList.Add("CLUNBR")

Dim X As String
Dim strExpression as String = ""
For Each X In fieldList
    If pTable.FindField(X) < 0 Then
        AddField(pTable, X) 'Adds the attribute to the end of the table
        Form.ShowDialog() 'User chooses from 3 radioButtons, selects from a comboBox (option 1),
                          'enters a value (option 2), or leaves the new field blank (option 3).
                          'User then clicks a button to close the form, and the code continues.

        If Form.rbCopyAttribute.Checked = True Then
            strExpression = String.Format("[{0}]", Form.cbCopyAttribute.SelectedItem.ToString)
        ElseIf Form.rbEnterValue.Checked = True Then
            strExpression = String.Format("""{0}""", CStr(Form.txtEnterValue.Text.ToString.Trim))
        ElseIf Form.rbLeaveBlank.Checked = True Then
            strExpression = """ """ 'This should represent a space
        Else
            Debug.Print("This should never occur.")
            Exit Sub
        End If

        'Populate the new field
        pParams.RemoveAll()
        pParams.Add(pTable)
        pParams.Add(X)
        Debug.Print("{0} = {1}", X, strExpression)
        pParams.Add(strExpression)
        pGP.Execute("CalculateField_management", pParams, Nothing) 'HRESULT error occurs here
    End If
Next
share|improve this question
Please post the code and identify the line on which the code raises the above error. – Jakub Oct 1 '12 at 13:39
You show the form, allow the user to make their choice then to return to the calling code you close the form. How is it that you are able to test the radio button checked status if you have closed the form? This may be your problem? – Hornbydd Oct 17 '12 at 14:31
The form wasn't closed until it was done getting the user choices for all of the attributes it was changing. I ended up re-writing the code so the form was opened & closed for each attribute. A little clunkier, but it worked as expected. – Bjorn Oct 30 '12 at 16:33

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.