I want to change some string field in sdegeodatabse with bellow code. The code are running without any problem. but after running, filed length return to default length. What am i missing?

static void writetablecode(IDataset dataset)
            ITable table = dataset as ITable;

            IFieldsEdit fields = table.Fields as IFieldsEdit;
            for (int i = 0; i < fields.FieldCount; i++)
                IFieldEdit field = fields.Field[i] as IFieldEdit;
                if (field.Type==esriFieldType.esriFieldTypeString & field.Length<30 )
                    field.Length_2  = 30;

  • 1
    did you put your code inside a IWorkspaceEdit editing block? and I think your code must be run under ArcCatalog extention.Anyway,if possible , provide more info about environment and pre-post running code,otherwise alot of things can prevent your code from doing what you want from it. – Reza Jun 20 '15 at 11:43
  • Not all data sources support the concept of width change. In fact, very few do. The usual mechanism is to add a new column with the desired width, calclate the values over, delete the old column, and rename the new one. – Vince Jun 20 '15 at 14:02
  • Hi, @Reza ,Yes i used Iworckspace edit and its not working yet. – Mahdi Ahmadi Jun 21 '15 at 4:19
  • @Vince , i can to change field length in ArcMap.But in arcobject that change not stored – Mahdi Ahmadi Jun 21 '15 at 4:19

Changing text fields length needs an exclusive schema lock on the dataset.

Exclusive schema locks—In contrast to shared schema locks, exclusive schema locks are controlled by the developer or ArcGIS application, such as ArcMap or ArcCatalog. An exclusive lock is used to lock a geodatabase dataset or object from use by others to make the necessary changes to it. An exclusive lock is promoted from a shared lock and demoted back to a shared lock when no longer needed. The presence of additional shared schema locks on a dataset or object prevents an exclusive schema lock from being applied and precludes the ability to make changes to the underlying dataset and its schema while it is in use. Only one exclusive schema lock is allowed per dataset. As opposed to shared schema locks, exclusive schema locks are not applied or removed automatically. It is the responsibility of the developer to apply or remove exclusive schema locks.

So, before running your code, make sure to close your dataset in Arcmap or ArcCatalog.

And then this code:

public void AlterField(IObjectClass objectClass, String fieldName)
    // Cast to the ISchemaLock interface.
    ISchemaLock schemaLock = (ISchemaLock)objectClass; 
        // Get an exclusive schema lock on the object class. 
        // Change the text field length here
    catch (Exception e)
        // An error was raised; therefore, notify the user.
        // Since the Finally block is always called, the exclusive lock is demoted
        // to a shared lock after the field is deleted and after an error is raised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.