IFieldsEdit.AddField() throws HRESULT: 0x80040705

238
4
07-25-2011 06:52 AM
Highlighted
Occasional Contributor
Hello,

I want to create a feature Class within a Geodatabase (the code below).
When computing
IFieldsEdit.AddField(IFieldEdit)
it crashes and returns
"Exception from HRESULT: 0x80040705"


It wouold be grateful if somebody could get any help or ideas to solve this problem.
Thanks!


Here are some object-values at runtime:

IFieldsEdit:
base {System.__ComObject} System.MarshalByRefObject {System.__ComObject}
[INDENT]__identity null object
Identity null object
m_ObjectToDataMap null System.Collections.Hashtable[/INDENT]
FieldCount 2 int


IFieldEdit:
oidFieldEdit {ESRI.ArcGIS.Geodatabase.FieldClass} ESRI.ArcGIS.Geodatabase.IFieldEdit {ESRI.ArcGIS.Geodatabase.FieldClass}
[INDENT][ESRI.ArcGIS.Geodatabase.FieldClass] {ESRI.ArcGIS.Geodatabase.FieldClass} ESRI.ArcGIS.Geodatabase.FieldClass
[INDENT]AliasName "OID" string
DataType {System.__ComObject} ESRI.ArcGIS.Geodatabase.IGPDataType {System.__ComObject}
DefaultValue {} dynamic {System.DBNull}
Domain null ESRI.ArcGIS.Geodatabase.IDomain
DomainFixed false bool
Editable false bool
GeometryDef null ESRI.ArcGIS.Geodatabase.IGeometryDef
IField2_AliasName "OID" string
IField2_DefaultValue {} dynamic {System.DBNull}
IField2_Domain null ESRI.ArcGIS.Geodatabase.IDomain
IField2_DomainFixed false bool
IField2_Editable false bool
IField2_GeometryDef null ESRI.ArcGIS.Geodatabase.IGeometryDef
IField2_IsNullable false bool
IField2_Length 4 int
IField2_Name "OID" string
IField2_Precision 0 int
IField2_Required true bool
IField2_Scale 0 int
IField2_Type esriFieldTypeOID ESRI.ArcGIS.Geodatabase.esriFieldType
IField2_VarType 3 int
IFieldEdit2_AliasName "OID" string
IFieldEdit2_DefaultValue {} dynamic {System.DBNull}
IFieldEdit2_Domain null ESRI.ArcGIS.Geodatabase.IDomain
IFieldEdit2_DomainFixed false bool
IFieldEdit2_Editable false bool
IFieldEdit2_GeometryDef null ESRI.ArcGIS.Geodatabase.IGeometryDef
IFieldEdit2_IsNullable false bool
IFieldEdit2_Length 4 int
IFieldEdit2_Name "OID" string
IFieldEdit2_Precision 0 int
IFieldEdit2_RasterDef null ESRI.ArcGIS.Geodatabase.IRasterDef
IFieldEdit2_Required true bool
IFieldEdit2_Scale 0 int
IFieldEdit2_Type esriFieldTypeOID ESRI.ArcGIS.Geodatabase.esriFieldType
IFieldEdit2_VarType 3 int
IFieldEdit_AliasName "OID" string
IFieldEdit_DefaultValue {} dynamic {System.DBNull}
IFieldEdit_Domain null ESRI.ArcGIS.Geodatabase.IDomain
IFieldEdit_DomainFixed false bool
IFieldEdit_Editable false bool
IFieldEdit_GeometryDef null ESRI.ArcGIS.Geodatabase.IGeometryDef
IFieldEdit_IsNullable false bool
IFieldEdit_Length 4 int
IFieldEdit_Name "OID" string
IFieldEdit_Precision 0 int
IFieldEdit_Required true bool
IFieldEdit_Scale 0 int
IFieldEdit_Type esriFieldTypeOID ESRI.ArcGIS.Geodatabase.esriFieldType
IFieldEdit_VarType 3 int
IsNullable false bool
Length 4 int
ModelName "OID" string
Name "OID" string
Precision 0 int
RasterDef null ESRI.ArcGIS.Geodatabase.IRasterDef
Required true bool
Scale 0 int
Type esriFieldTypeOID ESRI.ArcGIS.Geodatabase.esriFieldType
VarType 3 int[/INDENT]
AliasName "OID" string
DefaultValue {} dynamic {System.DBNull}
Domain null ESRI.ArcGIS.Geodatabase.IDomain
DomainFixed false bool
Editable false bool
GeometryDef null ESRI.ArcGIS.Geodatabase.IGeometryDef
IsNullable false bool
Length 4 int
Name "OID" string
Precision 0 int
Required true bool
Scale 0 int
Type esriFieldTypeOID ESRI.ArcGIS.Geodatabase.esriFieldType
VarType 3 int[/INDENT]



and this is the code i want to run:

public IFeatureClass CreateFeatureClassInWorkspace(String featureClassName, IFeatureWorkspace featureWorkspace)
        {
            try
            {
                // Instantiate a feature class description to get the required fields.
                IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
                IObjectClassDescription ocDescription = (IObjectClassDescription) fcDescription;
                IFields fields = ocDescription.RequiredFields;
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

                // Add an object ID field to the fields collection. This is mandatory for feature classes.
                IField oidField = new FieldClass();
                IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
                oidFieldEdit.Name_2 = "OID";
                oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
                fieldsEdit.AddField(oidFieldEdit);

                // Create a geometry definition (and spatial reference) for the feature class.
                IGeometryDef geometryDef = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
                ISpatialReference spatialReference =
                spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_20N);
                ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference;
                spatialReferenceResolution.ConstructFromHorizon();
                ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference;
                spatialReferenceTolerance.SetDefaultXYTolerance();
                geometryDefEdit.SpatialReference_2 = spatialReference;

                // Add a geometry field to the fields collection. This is where the geometry definition is applied.
                IField geometryField = new FieldClass();
                IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField;
                geometryFieldEdit.Name_2 = "Shape";
                geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                geometryFieldEdit.GeometryDef_2 = geometryDef;
                fieldsEdit.AddField(geometryField);

                // Add a Name text field to the required fields.
                IField field = new FieldClass();
                IFieldEdit fieldEdit = (IFieldEdit)field;
                fieldEdit.Name_2 = "Name";
                fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                fieldsEdit.AddField(field);

                // Use IFieldChecker to create a validated fields collection.
                IFieldChecker fieldChecker = new FieldCheckerClass();
                IEnumFieldError enumFieldError = null;
                IFields validatedFields = null;
                fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace;
                fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

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

                // Create the feature class.
                IFeatureClass featureClass = featureWorkspace.CreateFeatureClass
                  (featureClassName, validatedFields, ocDescription.InstanceCLSID,
                  ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple,
                  fcDescription.ShapeFieldName, "");
                return featureClass;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                throw e;
            }


Thanks.
Reply
0 Kudos
4 Replies
Highlighted
Occasional Contributor II
In .NET you need to append "_2" to the property you want to set on this interface. This is mentioned in the IFieldsEdit documentation.
Reply
0 Kudos
Highlighted
Regular Contributor III
It looks like you're adding the required fields with this line:

IFields fields = ocDescription.RequiredFields;

The OID is a required field so I imagine you're getting the error because you're trying to add it a second time with this code.

// Add an object ID field to the fields collection. This is mandatory for feature classes.
                IField oidField = new FieldClass();
                IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
                oidFieldEdit.Name_2 = "OID";
                oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
                fieldsEdit.AddField(oidFieldEdit);
Reply
0 Kudos
Highlighted
Occasional Contributor
Hi
thanks a lot.

Do you know which Fields are all Required Field?
I would expect that OID ans Shape are the required fields.
Or are there some more Fields?
Reply
0 Kudos
Highlighted
Regular Contributor III
Refer to the developer help topic for IObjectClassDescription.RequiredFields.  You can also loop through the Fields object it returns and see what fields it put in there.
Reply
0 Kudos