Personal GDB FeatureClass.CreateFeature() "Item not found in this collection." error

2624
10
03-04-2011 09:48 AM
ErikLee
New Contributor II
I create a personal geodatabase and then a FeatureClass in that gdb to store the results of an analysis. When I call <FeatureClass>.CreateFeature() I get an exception message "Item not found in this collection." I've ensured my fields are set correctly, I physically open the .mdb and the table looks correct. Here's my code (I'm using properties from an existing SDE featureclass (pipeFittingFeatureClass) to construct the new FeatureClass):

                //create the 'ServicePointCount' field
                IFieldEdit servicePointCountFieldEdit = new FieldClass();
                servicePointCountFieldEdit.Name_2 = "ServicePointCount";
                servicePointCountFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;

                //create ORIG_OID field
                IFieldEdit origOIDFieldEdit = new FieldClass();
                origOIDFieldEdit.Name_2 = "ORIG_OID";
                origOIDFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;

                //create OID field
                IFieldEdit OIDFieldEdit = new FieldClass();
                OIDFieldEdit.Name_2 = "ObjectID";
                OIDFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;

                //create shape field
                IFieldEdit shapeFieldEdit = new FieldClass();
                shapeFieldEdit.Name_2 = this.pipeFittingFeatureClass.ShapeFieldName;
                shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                IGeometryDef shapeFieldGeometryDefinition = new GeometryDefClass();
                IGeometryDefEdit shapeFieldGeometryDefinitionEdit = (IGeometryDefEdit)shapeFieldGeometryDefinition;
                shapeFieldGeometryDefinitionEdit.GeometryType_2 = this.pipeFittingFeatureClass.ShapeType;
                ISpatialReferenceFactory3 spatialRefFactory = new SpatialReferenceEnvironmentClass();
                ISpatialReference origRef = this.pipeFittingFeatureClass.Fields.get_Field(this.pipeFittingFeatureClass.Fields.FindField("SHAPE")).GeometryDef.SpatialReference;
                shapeFieldGeometryDefinitionEdit.SpatialReference_2 = spatialRefFactory.ConstructHighPrecisionSpatialReference(origRef, -1, 0, 0);
                shapeFieldEdit.GeometryDef_2 = shapeFieldGeometryDefinitionEdit;

                //create new fields class
                IFieldsEdit fieldsEdit = new FieldsClass();
                fieldsEdit.FieldCount_2 = 4;//this.pipeFittingFeatureClass.Fields.FieldCount + 2;
                fieldsEdit.set_Field(0, (IField)OIDFieldEdit);
                fieldsEdit.set_Field(1, (IField)origOIDFieldEdit);
                fieldsEdit.set_Field(2, (IField)servicePointCountFieldEdit);
                fieldsEdit.set_Field(3, (IField)shapeFieldEdit);

                //create the featureclass
                IFeatureClass EFVValvesFeatureClass = workspace.CreateFeatureClass("EFVValves", (IFields)fieldsEdit, null,
                    null, this.pipeFittingFeatureClass.FeatureType, this.pipeFittingFeatureClass.ShapeFieldName, "");

                //THIS THROWS THE "Item not found in this collection." EXCEPTION
                IFeature newFeature = EFVValvesFeatureClass.CreateFeature();


I tried searching for this error and got a few hits but none of the fixes applied to my situation so I am at a loss as to ideas. My suspicion is the how I define the Shape field, but if I do not create a high precision spatial reference (the template spatial reference I use is low), it won't even allow me to create the featureclass.

For reference, I'm using ArcGIS 9.3.1 Sp1, and this is just a small console app that does some custom tracing and stores the results in this personal GDB.
0 Kudos
10 Replies
NeilClemmons
Regular Contributor III
Have you tried get a fresh reference to the feature class by calling OpenFeatureClass instead of using the reference returned by CreateFeatureClass?
0 Kudos
ErikLee
New Contributor II
I just tried it and I get the same error. I even tried re-opening the workspace and then opening the featureclass, but again getting the same error.
0 Kudos
GagagDa_Morvi
New Contributor
I create a personal geodatabase and then a FeatureClass in that gdb to store the results of an analysis. When I call <FeatureClass>.CreateFeature() I get an exception message "Item not found in this collection." I've ensured my fields are set correctly, I physically open the .mdb and the table looks correct. Here's my code (I'm using properties from an existing SDE featureclass (pipeFittingFeatureClass) to construct the new FeatureClass):

                //create the 'ServicePointCount' field
                IFieldEdit servicePointCountFieldEdit = new FieldClass();
                servicePointCountFieldEdit.Name_2 = "ServicePointCount";
                servicePointCountFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;

                //create ORIG_OID field
                IFieldEdit origOIDFieldEdit = new FieldClass();
                origOIDFieldEdit.Name_2 = "ORIG_OID";
                origOIDFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;

                //create OID field
                IFieldEdit OIDFieldEdit = new FieldClass();
                OIDFieldEdit.Name_2 = "ObjectID";
                OIDFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;

                //create shape field
                IFieldEdit shapeFieldEdit = new FieldClass();
                shapeFieldEdit.Name_2 = this.pipeFittingFeatureClass.ShapeFieldName;
                shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                IGeometryDef shapeFieldGeometryDefinition = new GeometryDefClass();
                IGeometryDefEdit shapeFieldGeometryDefinitionEdit = (IGeometryDefEdit)shapeFieldGeometryDefinition;
                shapeFieldGeometryDefinitionEdit.GeometryType_2 = this.pipeFittingFeatureClass.ShapeType;
                ISpatialReferenceFactory3 spatialRefFactory = new SpatialReferenceEnvironmentClass();
                ISpatialReference origRef = this.pipeFittingFeatureClass.Fields.get_Field(this.pipeFittingFeatureClass.Fields.FindField("SHAPE")).GeometryDef.SpatialReference;
                shapeFieldGeometryDefinitionEdit.SpatialReference_2 = spatialRefFactory.ConstructHighPrecisionSpatialReference(origRef, -1, 0, 0);
                shapeFieldEdit.GeometryDef_2 = shapeFieldGeometryDefinitionEdit;

                //create new fields class
                IFieldsEdit fieldsEdit = new FieldsClass();
                fieldsEdit.FieldCount_2 = 4;//this.pipeFittingFeatureClass.Fields.FieldCount + 2;
                fieldsEdit.set_Field(0, (IField)OIDFieldEdit);
                fieldsEdit.set_Field(1, (IField)origOIDFieldEdit);
                fieldsEdit.set_Field(2, (IField)servicePointCountFieldEdit);
                fieldsEdit.set_Field(3, (IField)shapeFieldEdit);

                //create the featureclass
                IFeatureClass EFVValvesFeatureClass = workspace.CreateFeatureClass("EFVValves", (IFields)fieldsEdit, null,
                    null, this.pipeFittingFeatureClass.FeatureType, this.pipeFittingFeatureClass.ShapeFieldName, "");

                //THIS THROWS THE "Item not found in this collection." EXCEPTION
                IFeature newFeature = EFVValvesFeatureClass.CreateFeature();
I tried searching for this error and got a few hits but none of the fixes applied to my situation so I am at a loss as to ideas. My suspicion is the how I define the Shape field, but if I do not create a high precision spatial reference (the template spatial reference I use is low), it won't even allow me to create the featureclass.

For reference, I'm using ArcGIS 9.3.1 Sp1, and this is just a small console app that does some custom tracing and stores the results in this personal GDB.


I have had this error in the past - almost always the problem was with the incorrect field name. Check all the field names in your code, number of fields being set, etc. etc.
0 Kudos
ErikLee
New Contributor II
I have had this error in the past - almost always the problem was with the incorrect field name. Check all the field names in your code, number of fields being set, etc. etc.


Yeah I get that feeling too, like it's some type of error in the fields collection, but I just don't see it. I crete an OID field, the 2 data fields, and a shape field, set the fields count to 4, assign the fields. Is there some type of naming convention I'm breaking? I know for shapefiles the names can't be longer than 8 chars, but I'm unaware of any naming limitations for an access GDB.

    IFieldsEdit fieldsEdit = new FieldsClass();
    fieldsEdit.FieldCount_2 = 4;
    fieldsEdit.set_Field(0, (IField)OIDFieldEdit);
    fieldsEdit.set_Field(1, (IField)origOIDFieldEdit);
    fieldsEdit.set_Field(2, (IField)servicePointCountFieldEdit);
    fieldsEdit.set_Field(3, (IField)shapeFieldEdit);
0 Kudos
NeilClemmons
Regular Contributor III
Access does have a limit of 64 characters for field names (and shapefiles are 10 chars, not 😎 but I doubt that's your problem.  You would get an error when creating the feature class, not when trying to create a feature.  Can you create a new feature using the ArcMap Editor?  It's a long shot but it may give you a better error message if it can't create the feature.  Also, have you used ArcCatalog to examine the spatial reference properties (xy domain, tolerance, resolution, etc.)?  The developer help topic for IFeatureWorkspace.CreateFeatureClass has several examples that you may want to take a look at as well.  They show how to use the FieldChecker class to validate your fields collection.
0 Kudos
ErikLee
New Contributor II
Thanks for the ideas Neil!

I just used the ArcMap editor to create a feature and it worked just fine.

In ArcCatalog, when I open the Properties window of the featureclass inside the .mbd and select the "Weight Association" tab ArcCatalog crashes. Not sure if this is a known bug or if it's even related to my issue, but it's interesting nonetheless.

XY tolerance is 0.008285132265221
XY Resolution is 0.000517820766576
XY Domain:
   Max Y:4664015283195.6
   Min  Y: -99539599.9983669
   Max X: 4663997718495.6
   Min X: -117104299.997082

Looks OK to me, but then again I don't know much about spatial projections. I just clone what the source featureclass uses, then convert it to high precision as detailed in the code snippet in my original post.

I will investigate the FieldsChecker class that you mentioned, it sounds promising.
0 Kudos
ErikLee
New Contributor II
Unfortunately, the FieldChecker revealed no issues. I'm not sure if the IEnumFieldError out parameter of the Validate method is supposed to be null if no errors are found, but being an 'out' parameter I had assumed it was guaranteed not to be null.
0 Kudos
NeilClemmons
Regular Contributor III
You mentioned this is a console application.  Are you checking out a license before running any of this code?  Are you running this code inside of an edit session?  If so, you shouldn't be creating new feature classes while in an edit session.  Also, once the feature class has been created and your code errors out, can you shut down your application then go back and create a feature through code?
0 Kudos
ErikLee
New Contributor II
Thanks for sticking with me on this Neil. I do check out a license (ArcInfo) before using any ArcObjects. I'm not in an edit session. And yes, When the app throws the error I can open ArcMap, add the newly created Access GDB Featureclass, edit (create features) and save with no problems.

I just created a Shapefile in the same manner (with the exact same IFields object) and that works fine.
0 Kudos