I am trying to add a new field and fill the field for some of rows but I face with the following error:
"The index passed was not within the valid range."
My simplified codes:
IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
IMap map = mxdoc.FocusMap;
ILayer layer = map.Layer;
IFeatureLayer2 featureLayer = layer as IFeatureLayer2;
IFeatureClass flFeatureClass = featureLayer.FeatureClass;
IFeatureCursor featureCursor = flFeatureClass.Search(null, true);
IFeature feature2 = featureCursor.NextFeature();
IFieldEdit2 newField = new FieldClass();
newField.Name_2 = "name";
newField.Editable_2 = true;
newField.Type_2 = esriFieldType.esriFieldTypeString;
while (feature2 != null)
feature2 = featureCursor.NextFeature();
When i used "breakpoint" to find the error line. i found out that
is the line that causes the error (The index passed was not within the valid range).
I do not know what is wrong with set_value method. when i try to update a already created field, it work right but
when i want to create a new field and fill it with some data as mentioned codes , i encounter with the error.
Thanks in advance
Create the field: ArcObjects Help for .NET developers
Next use a cursor to update the content of the field: ArcObjects Help for .NET developers :
Make sure you determine the index of the field outside the loop (line 4), to avoid doing this for each feature.
// Use IFeatureClass.Update to populate IFeatureCursor.
IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);
int typeFieldIndex = featureClass.FindField("name");
IFeature feature = null;
while ((feature = updateCursor.NextFeature()) != null)
catch (COMException comExc)
// Handle any errors that might occur on NextFeature().
// If the cursor is no longer needed, release it.