Problem with creating new feature class in Personal or File Geodatabase

Discussion created by mdennah on Jul 26, 2010
Latest reply on Aug 3, 2010 by kenbuja
Hi everyone,

I am trying to  programmatically ( create new feature class in Personal or File Geodatabase, but even though I am doing my best, I am not able to find a mistake.
The code below works great for creating Shapefiles and I don't know how to change it for FC in geodatabase.  The problem seems to be in a part for Field creating, but don't know where.

Does anybody have idea? Thanks for all advices.

Private Sub CreateFeature()
        Dim pWorkSpaceFactory1 As IWorkspaceFactory2
        'Dim pWorkspaceName As IWorkspaceName
        Dim pFeatureWorkspace1 As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace

        Const strTemp As String = "c:\"
        Const strShapeFieldName As String = "Shape"
        Const strFeatureClassName As String = "NewFeatureClass1"

        ' For shapefiles - works fine
        pWorkSpaceFactory1 = New ShapefileWorkspaceFactory 'Creates workspace for shapefiles
        pFeatureWorkspace1 = pWorkSpaceFactory1.OpenFromFile(strTemp, 0) 'Opens workspace for shapefiles

        ''For FC in Geodatabase - doesn't work 
        'pWorkSpaceFactory1 = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory() 'Creates workspace for personal geodatabase
        '    pWorkspaceName = pWorkSpaceFactory1.Create(strTemp, "temp", Nothing, 0) 'Creates Personal Geodatabase
        'Catch ex As Exception
        'End Try
        'pFeatureWorkspace1 = pWorkSpaceFactory1.OpenFromFile(strTemp & "temp.mdb", 0) 'Opens workspace for personal geodatabase

        Dim pFields As IFields = New FieldsClass
        Dim pFieldsEdit As IFieldsEdit = CType(pFields, IFieldsEdit)
        pFieldsEdit.FieldCount_2 = 3

        Dim pField As IField
        Dim pFieldEdit As IFieldEdit

        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)
        pFieldEdit.Name_2 = "OBJECTID"
        pFieldEdit.AliasName_2 = "OBJECT ID"
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID
        pFieldsEdit.Field_2(0) = pField

        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)

        Dim geometryDef As IGeometryDef = New GeometryDefClass
        Dim geometryDefEdit As IGeometryDefEdit = CType(geometryDef, IGeometryDefEdit)
        geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline
        geometryDefEdit.GridCount_2 = 1
        geometryDefEdit.HasM_2 = False
        geometryDefEdit.HasZ_2 = False

        pFieldEdit.Name_2 = strShapeFieldName
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry
        pFieldEdit.GeometryDef_2 = geometryDef
        pFieldEdit.IsNullable_2 = True
        pFieldEdit.Required_2 = True
        pFieldsEdit.Field_2(1) = pField

        pField = New Field
        pFieldEdit = CType(pField, IFieldEdit)
        pFieldEdit.Name_2 = "txt_field"
        pFieldEdit.AliasName_2 = "Field for text information"
        pFieldEdit.Editable_2 = True
        pFieldEdit.IsNullable_2 = False
        pFieldEdit.Precision_2 = 2
        pFieldEdit.Scale_2 = 5
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble
        pFieldsEdit.Field_2(2) = pField

        pFeatureWorkspace1.CreateFeatureClass(strFeatureClassName, pFields, Nothing, Nothing, esriFeatureType.esriFTSimple, strShapeFieldName, Nothing)

    End Sub