error: no valid index range

4485
8
01-23-2015 01:34 PM
MariaMaldini
New Contributor

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

0 Kudos
8 Replies
MariaMaldini
New Contributor

hello guys, there is not anyone who helps me? i could not find any helpful material about this problem on internet. please help me

0 Kudos
XanderBakker
Esri Esteemed Contributor

You should first add the field and then start the cursor.

0 Kudos
MariaMaldini
New Contributor

if you look carefully you will find out that i created the field before populating it.

0 Kudos
MariaMaldini
New Contributor

guys my workspace is  .shp file and i am using ArcGIS 10 with arcinfo license.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Your featureclass is a shapefile. Your workspace in that case is a folder.

0 Kudos
XanderBakker
Esri Esteemed Contributor

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;
try
{
    while ((feature = updateCursor.NextFeature()) != null)
    {
        feature.set_Value(typeFieldIndex, "rome");
        updateCursor.UpdateFeature(feature);
    }
}
catch (COMException comExc)
{
    // Handle any errors that might occur on NextFeature().
}

// If the cursor is no longer needed, release it.
Marshal.ReleaseComObject(updateCursor);
MariaMaldini
New Contributor

Thanks Xander for your attention,

0 Kudos
XanderBakker
Esri Esteemed Contributor

... and if you read my answer carefully, you would have noticed that I referred to adding the field before creating the cursor...

0 Kudos