' Here it gives mistake ' Add the field. featureClass2.AddField(fields)
featureClass2.AddField(fields) Dim classSchemaEdit As ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3 classSchemaEdit = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3) Dim schemaLock As ESRI.ArcGIS.Geodatabase.ISchemaLock schemaLock = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.ISchemaLock) schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriExclusiveSchemaLock) classSchemaEdit.AlterFieldAliasName("Tipo_punto", "Tipo_max_min") schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriSharedSchemaLock)
The mistake in the creation of the fields already is corrected. I answer the featureclass of the existing one and I him add the necessary fields. The problem is that it does not take the Alias of the new fields: short2FieldEdit.AliasName_2 = "Tipo_max_min" short2FieldEdit.Name_2 = "Tipo_punto"I do not know like add the new information in the created fields.Thanks a lot Doris.Regards, Héctor.
Thanks a lot feng.zhang.esri.It continues without me working. On having come to the new code it gives the following mistake:"It is not possible to turn the object COM of the type ' System. __ ComObject ' to the type of interface ' ESRI.ArcGIS.Geodatabase. IClassSchemaEdit3 '. A mistake of operation happened due to the fact that the so called QueryInterface in the component COM for the interface with IID ' {4B35F814-0417-47E3-8DFC-CAD58746693B} ' generated the following mistake: not compatible Interface (HRESULT's Exception: 0x80004002 (E_NOINTERFACE))."Also I have tried with CType instead of DirectCast and neither it works.regards.
Please try to replace the following two lines:Dim classSchemaEdit As ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3classSchemaEdit = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3)with Dim classSchemaEdit As IClassSchemaEdit3 = TryCast(featureClass2, IClassSchemaEdit3)
Sub AddFieldtoFeature(ByVal FeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer, ByVal FieldName As String, ByVal FieldType As ESRI.ArcGIS.Geodatabase.esriFieldType, ByVal scale As Integer, ByVal Precision As Integer, ByVal Length As Integer, ByVal IsNullable As Boolean, ByVal AliasName As String, ByVal DefaultValue As String) Dim pMxdoc As ESRI.ArcGIS.ArcMapUI.IMxDocument Dim pMap As ESRI.ArcGIS.Carto.IMap Dim pApp As ESRI.ArcGIS.Framework.IApplication pApp = m_application pMxdoc = m_application.Document pMap = pMxdoc.FocusMap ' the active map the button was pressed, use Approt if you want to connect to different map Dim pFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass = FeatureLayer.FeatureClass Dim pField As ESRI.ArcGIS.Geodatabase.IFieldEdit2 pField = New ESRI.ArcGIS.Geodatabase.Field Dim pCheckField As ESRI.ArcGIS.Geodatabase.IFields Dim CheckFieldInt As Integer Try pCheckField = pFeatureClass.Fields CheckFieldInt = pCheckField.FindField(FieldName) If CheckFieldInt = -1 Then ' lets make sure we are OUT OF editing but do this so if all fields are created... we can stay in edit mode: Dim pID As New ESRI.ArcGIS.esriSystem.UID pID.Value = "esriEditor.Editor" ' PROG ID 'or the CLSID 'pID.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}" Dim pEditor As ESRI.ArcGIS.Editor.IEditor pEditor = pApp.FindExtensionByCLSID(pID) If pEditor.EditState = ESRI.ArcGIS.Editor.esriEditState.esriStateEditing Then ' if in edit mode... Dim pUID As New ESRI.ArcGIS.esriSystem.UID pUID.Value = "{59D2AFD1-9EA2-11D1-9165-0080C718DF97}" 'STOP EDITING, MUST HAVE {} Dim pCommandBars As ESRI.ArcGIS.Framework.ICommandBars Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem pCommandBars = pApp.Document.CommandBars pCommandItem = pCommandBars.Find(pUID) pCommandItem.Execute() 'start editing "{59D2AFD0-9EA2-11D1-9165-0080C718DF97}" System.Threading.Thread.Sleep(100) End If pField.Name_2 = FieldName pField.Type_2 = FieldType 'Select appropriately based on field type selected If FieldType = esriFieldType.esriFieldTypeDouble Or FieldType = esriFieldType.esriFieldTypeInteger Or FieldType = esriFieldType.esriFieldTypeSmallInteger Then pField.Scale_2 = scale pField.Precision_2 = Precision End If If FieldType = esriFieldType.esriFieldTypeString Then pField.Length_2 = Length End If If IsNullable <> Nothing Then pField.IsNullable_2 = IsNullable End If If AliasName <> "" Then pField.AliasName_2 = AliasName End If If DefaultValue <> "" Then pField.DefaultValue_2 = DefaultValue End If pFeatureClass.AddField(pField) End If Catch ex As Exception MessageBox.Show("Failed to create field" & vbNewLine & ex.Message & vbNewLine & "Error Details:" & vbNewLine & ex.StackTrace, "Create Feature Field", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
It is beeter to use geoProcessing tools instead of writing our own code if one is available in GPTools. Geoprocessor gProcess = new Geoprocessor(); IVariantArray varArray = new VarArrayClass(); varArray.Add(@...\MyPGDB.mdb\DatasetTwo\Line_2); varArray.Add("MyNewField"); varArray.Add("TEXT"); gProcess.Execute("AddField", varArray, null);
I had written a function which is generic, allowing you to create various fields. Just call the AddFieldtoFeature in your main code block. It does check if the field already exists. Yes, the GP works as well, but I prefer this way... full control. You would just call by: AddFieldtoFeature(pFeaturelayer, "FIELDNAME", esriFieldType.esriFieldTypeString, Nothing, Nothing, 20, Nothing, "ALIAS", Nothing) **This works by passing the appropriate information, any info that is to be left out needs to be NOTHING , "" will not work. Copy the entire code below and place somewhere in the Class. you will have to reference the pMxDoc yourself. (m_Application) Sub AddFieldtoFeature(ByVal FeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer, ByVal FieldName As String, ByVal FieldType As ESRI.ArcGIS.Geodatabase.esriFieldType, ByVal scale As Integer, ByVal Precision As Integer, ByVal Length As Integer, ByVal IsNullable As Boolean, ByVal AliasName As String, ByVal DefaultValue As String) Dim pMxdoc As ESRI.ArcGIS.ArcMapUI.IMxDocument Dim pMap As ESRI.ArcGIS.Carto.IMap Dim pApp As ESRI.ArcGIS.Framework.IApplication pApp = m_application pMxdoc = m_application.Document pMap = pMxdoc.FocusMap ' the active map the button was pressed, use Approt if you want to connect to different map Dim pFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass = FeatureLayer.FeatureClass Dim pField As ESRI.ArcGIS.Geodatabase.IFieldEdit2 pField = New ESRI.ArcGIS.Geodatabase.Field Dim pCheckField As ESRI.ArcGIS.Geodatabase.IFields Dim CheckFieldInt As Integer Try pCheckField = pFeatureClass.Fields CheckFieldInt = pCheckField.FindField(FieldName) If CheckFieldInt = -1 Then ' lets make sure we are OUT OF editing but do this so if all fields are created... we can stay in edit mode: Dim pID As New ESRI.ArcGIS.esriSystem.UID pID.Value = "esriEditor.Editor" ' PROG ID 'or the CLSID 'pID.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}" Dim pEditor As ESRI.ArcGIS.Editor.IEditor pEditor = pApp.FindExtensionByCLSID(pID) If pEditor.EditState = ESRI.ArcGIS.Editor.esriEditState.esriStateEditing Then ' if in edit mode... Dim pUID As New ESRI.ArcGIS.esriSystem.UID pUID.Value = "{59D2AFD1-9EA2-11D1-9165-0080C718DF97}" 'STOP EDITING, MUST HAVE {} Dim pCommandBars As ESRI.ArcGIS.Framework.ICommandBars Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem pCommandBars = pApp.Document.CommandBars pCommandItem = pCommandBars.Find(pUID) pCommandItem.Execute() 'start editing "{59D2AFD0-9EA2-11D1-9165-0080C718DF97}" System.Threading.Thread.Sleep(100) End If pField.Name_2 = FieldName pField.Type_2 = FieldType 'Select appropriately based on field type selected If FieldType = esriFieldType.esriFieldTypeDouble Or FieldType = esriFieldType.esriFieldTypeInteger Or FieldType = esriFieldType.esriFieldTypeSmallInteger Then pField.Scale_2 = scale pField.Precision_2 = Precision End If If FieldType = esriFieldType.esriFieldTypeString Then pField.Length_2 = Length End If If IsNullable <> Nothing Then pField.IsNullable_2 = IsNullable End If If AliasName <> "" Then pField.AliasName_2 = AliasName End If If DefaultValue <> "" Then pField.DefaultValue_2 = DefaultValue End If pFeatureClass.AddField(pField) End If Catch ex As Exception MessageBox.Show("Failed to create field" & vbNewLine & ex.Message & vbNewLine & "Error Details:" & vbNewLine & ex.StackTrace, "Create Feature Field", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.