HRESULT E_FAIL Error with trying to create feature class using ArcGIS Code Snippet

Discussion created by gary.gealy on Jun 23, 2011
I am getting a HRESULT error, I am using the Code Snippet from the ArcGIS SDK.  Any ideas why this is failing?  I am running this code from a toolbar and button that I created.  The exact errorr message is "Error HRESULT E_FAIL has been returned fron a cll to a COM component"

Dim pFWS As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace
Dim pWorkspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory
pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory
pFWS = pWorkspaceFactory.OpenFromFile("C:\tmp", 0)

pFeatClass = CreateFeatureClass(pFWS, Nothing, "name", Nothing, Nothing, Nothing, "gg")

#Region "Create FeatureClass"

    '''<summary>Simple helper to create a featureclass in a geodatabase.</summary>
    '''<param name="workspace">An IWorkspace2 interface</param>
    '''<param name="featureDataset">An IFeatureDataset interface or Nothing</param>
    '''<param name="featureClassName">A System.String that contains the name of the feature class to open or create. Example: "states"</param>
    '''<param name="fields">An IFields interface</param>
    '''<param name="CLSID">A UID value or Nothing. Example "esriGeoDatabase.Feature" or Nothing</param>
    '''<param name="CLSEXT">A UID value or Nothing (this is the class extension if you want to reference a class extension when creating the feature class).</param>
    '''<param name="strConfigKeyword">An empty System.String or RDBMS table string for ArcSDE. Example: "myTable" or ""</param>
    '''<returns>An IFeatureClass interface or a Nothing</returns>
    '''  (1) If a 'featureClassName' already exists in the workspace a reference to that feature class
    '''      object will be returned.
    '''  (2) If an IFeatureDataset is passed in for the 'featureDataset' argument the feature class
    '''      will be created in the dataset. If a Nothing is passed in for the 'featureDataset'
    '''      argument the feature class will be created in the workspace.
    '''  (3) When creating a feature class in a dataset the spatial reference is inherited
    '''      from the dataset object.
    '''  (4) If an IFields interface is supplied for the 'fields' collection it will be used to create the
    '''      table. If a Nothing value is supplied for the 'fields' collection, a table will be created using
    '''      default values in the method.
    '''  (5) The 'strConfigurationKeyword' parameter allows the application to control the physical layout
    '''      for this table in the underlying RDBMS???for example, in the case of an Oracle database, the
    '''      configuration keyword controls the tablespace in which the table is created, the initial and
    '''     next extents, and other properties. The 'strConfigurationKeywords' for an ArcSDE instance are
    '''      set up by the ArcSDE data administrator, the list of available keywords supported by a workspace
    '''      may be obtained using the IWorkspaceConfiguration interface. For more information on configuration
    '''      keywords, refer to the ArcSDE documentation. When not using an ArcSDE table use an empty
    '''      string (ex: "").
    Public Function CreateFeatureClass(ByVal workspace As IWorkspace2, ByVal featureDataset As IFeatureDataset, ByVal featureClassName As String, ByVal fields As IFields, ByVal CLSID As ESRI.ArcGIS.esriSystem.UIDClass, ByVal CLSEXT As ESRI.ArcGIS.esriSystem.UIDClass, ByVal strConfigKeyword As String) As IFeatureClass

        If featureClassName = "" Then
            Return Nothing ' name was not passed in
        End If

        Dim featureClass As IFeatureClass
        Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace) ' Explicit Cast

        If workspace.NameExists(esriDatasetType.esriDTFeatureClass, featureClassName) Then
            featureClass = featureWorkspace.OpenFeatureClass(featureClassName) ' feature class with that name already exists
            Return featureClass
        End If

        ' assign the class id value if not assigned
        If CLSID Is Nothing Then
            CLSID = New ESRI.ArcGIS.esriSystem.UIDClass
            CLSID.Value = "esriGeoDatabase.Feature"
        End If

        Dim objectClassDescription As IObjectClassDescription = New ObjectClassDescription 'FeatureClassDescriptionClass

        ' if a fields collection is not passed in then supply our own
        If fields Is Nothing Then

            ' create the fields using the required fields method
            fields = objectClassDescription.RequiredFields
            Dim fieldsEdit As IFieldsEdit = CType(fields, IFieldsEdit) ' Explict Cast
            Dim field As IField = New Field 'FieldClass

            ' create a user defined text field
            Dim fieldEdit As IFieldEdit = CType(field, IFieldEdit) ' Explict Cast

            ' setup field properties
            fieldEdit.Name_2 = "SampleField"
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeString
            fieldEdit.IsNullable_2 = True
            fieldEdit.AliasName_2 = "Sample Field Column"
            fieldEdit.DefaultValue_2 = "test"
            fieldEdit.Editable_2 = True
            fieldEdit.Length_2 = 100

            ' add field to field collection
            fields = CType(fieldsEdit, IFields) ' Explicit Cast

        End If

        Dim strShapeField As String = ""

        ' locate the shape field

        For j As Int32 = 0 To fields.FieldCount - 1
            If fields.Field(j).Type = esriFieldType.esriFieldTypeGeometry Then
                strShapeField = fields.Field(j).Name
            End If
        Next j

        ' Use IFieldChecker to create a validated fields collection.
        Dim fieldChecker As IFieldChecker = New FieldChecker  ' FieldCheckerClass()
        Dim enumFieldError As IEnumFieldError = Nothing
        Dim validatedFields As IFields = Nothing
        fieldChecker.ValidateWorkspace = CType(workspace, IWorkspace)
        fieldChecker.Validate(fields, enumFieldError, validatedFields)

        ' The enumFieldError enumerator can be inspected at this point to determine
        ' which fields were modified during validation.

            ' finally create and return the feature class
            If featureDataset Is Nothing Then
                ' if no feature dataset passed in, create at the workspace level
                featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword)
                featureClass = featureDataset.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword)
            End If

        Catch ex As Exception
        End Try
        Return featureClass

    End Function
#End Region