add field

2516
1
02-24-2011 09:27 PM
sujaygarapati
New Contributor
Hi,i am trying to add field using GP in arc engine but unable to add field, please help me how to add field, below is my code

Private Sub CreateField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateField.Click
      

        Dim text As String = TextBox1.Text
        TextBox1.Text = text

        'get Field name from textbox
        Dim FName As String = FieldNameTxtB.Text
        FieldNameTxtB.Text = FName
        'get Field name from textbox
        Dim FAlias As String = FieldAliasTxtB.Text
        FieldAliasTxtB.Text = FAlias
        'get field type from combobox
        Dim FType As String = FieldTypeCMB.Text
        FieldTypeCMB.Text = FType
        'get field lenght as integer from textbox
        Dim FLength As Integer = Convert.ToInt32(FieldLengthTxtB.Text)



        Dim addfield As ESRI.ArcGIS.DataManagementTools.AddField = New ESRI.ArcGIS.DataManagementTools.AddField()

        addfield.in_table = text
        addfield.field_name = FName
        addfield.field_alias = FAlias
        addfield.field_type = FType
        addfield.field_length = FLength

        Dim pddfield As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
        pddfield.Execute(addfield, Nothing)
        MessageBox.Show("Completed")



    End Sub
0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor
Have you explored the Geoprocessor results to see what is happening with your tool? Here's the Add Field function I use in one of my ArcMap projects. In it, I have created a Global variable for the geoprocessor and have initiated it elsewhere.

AddField(pTable, "Name", "Text", , , 254)

    Public Sub AddField(ByVal Input As Object, ByVal FieldName As String, ByVal FieldType As String, Optional ByVal Precision As Long = 0,
                        Optional ByVal Scale As Long = 0, Optional ByVal Length As Long = 0, Optional ByVal FieldAlias As String = Nothing,
                        Optional ByVal Nullable As String = "NULLABLE", Optional ByVal Required As String = "NON_REQUIRED", Optional ByVal Domain As String = Nothing)

        Dim AddField As New ESRI.ArcGIS.DataManagementTools.AddField
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(AddField)

                AddField.in_table = Input
                AddField.field_name = FieldName
                AddField.field_type = FieldType
                AddField.field_precision = Precision
                AddField.field_scale = Scale
                AddField.field_length = Length
                AddField.field_alias = FieldAlias
                AddField.field_is_nullable = Nullable
                AddField.field_is_required = Required
                AddField.field_domain = Domain

                Result = RunTool(AddField, Nothing)
                If Result Is Nothing Then
                    System.Windows.Forms.MessageBox.Show("Could not add field")
                End If
            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Add Field error")
        End Try

    End Sub

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Result = CType(Globals.GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
            Globals.GP.ClearMessages()
        Catch ex As Exception
            ReturnMessages(Result, "Fail")
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
        End Try

        Return Result

    End Function

   Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String)

        Dim ErrorMessage As String

        If pResult.MessageCount > 0 Then
            For Count As Integer = 0 To pResult.MessageCount - 1
                ErrorMessage += pResult.GetMessage(Count)
            Next
        End If

        System.Windows.Forms.MessageBox.Show(ErrorMessage, Title)

    End Sub

0 Kudos