[GIS] Add a new field in a already created feature class

arcgis-10.0cerrorfeature-class

I have a feature class. On the basis of some calculation, I want to create a new field in that table and set value of that field. To do so, my algorithm is:

void UpdateFeatureClass(IFeatureClass featureClass, List<Edge> edges)
{
    CreateField(featureClass);
    UpdateDictionary(featureClass);
    UpdateFieldValue(edges);
}

To add a field I use following code snippet:

private void CreateField(IFeatureClass featureClass)
{
    IFields oldFields = featureClass.Fields;
    IFieldsEdit fieldsEdit = (IFieldsEdit)oldFields;

    IField field = new FieldClass();
    var fieldEdit = (IFieldEdit)field;
    fieldEdit.Name_2 = "Value";
    fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
    fieldsEdit.AddField(field);
}

Update dictionary basically creates a dictionary with all features of the feature class. The key of the dictionary is feature's OID and value of the dictionary is the feature. After that I am trying to update features with value in the value field. The code snippet is:

private void UpdateFieldValue(List<Edge> edges)
{
    int fieldIndex = dictionary[0].Fields.FindField("Value");
    if (fieldIndex != -1)
    {
        foreach (Edge edge in edges)
        {
            var feature = _dictionary[edge.PolylineEdge.OID];
            feature.set_Value(fieldIndex, edge.Value);
            feature.Store();
        }
    }
}

Everything works fine for me except the feature.Store() command. Whenever this line executes, 'System.Runtime.InteropServices.COMException' exception occurs. From my perspective, if there is any problem with set value, the error should occur in that statement. I couldn't find out any pointer why the store command creates the error. Can anyone please help me to find why the error is occurring?

EDIT :
I tried to store value in different fields of the feature and succeeded. Only updating the value of the newly created field, gives the exception.

  • Error message: "Error encoding INFO item during save"
  • Error code: (-2147220133)

EDIT :
I think the problem is the newly created field is not permanent. after adding the field, I go through the feature class and the newly created field is here. But whenever I close the ArcMap and open a new ArcMap, I find that the newly added field is vanished. So, I think the problem is the change is not permanent. So, My question is how to create a field in a already created feature class permanently?

Thanks

Best Answer

To create a new field I think you need to have a schema lock first (ISchemaLock)

From the example in here:

public void AddFieldToFeatureClass(IFeatureClass featureClass, IField field)
{
    ISchemaLock schemaLock = (ISchemaLock)featureClass;

    try
    {
        // A try block is necessary, as an exclusive lock might not be available.
        schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

        // Add the field.
        featureClass.AddField(field);
    }
    catch (Exception exc)
    {
        // Handle appropriately for your application.
        Console.WriteLine(exc.Message);
    }
    finally
    {
        // Set the lock to shared, whether or not an error occurred.
        schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
    }
}
Related Question