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[0];
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;
flFeatureClass.AddField(newField);
while (feature2 != null)
{
feature2.set_Value(flFeatureClass.FindField("name"), "rome");
feature2.Store();
feature2 = featureCursor.NextFeature();
}
When i used "breakpoint" to find the error line. i found out that
feature2.set_Value(flFeatureClass.FindField("name"), "rome");
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