0

I want to change data type of a field to type esriFieldTypeOID in a layer. So i copy existing field into a new field than delete existing field first and than change datatype for copied field and than add copied filed with new data type as new field. Please do note that currently that field holds values in it. But below code throws an error saying "A syntax error occurred trying to evaluate a query string (Exception from HRESULT: 0x80040203)" as soon as DeleteField function is called.

 IDocument doc = ArcMap.Application.Document;
        IMxDocument mxDoc = doc as IMxDocument;
        IMap map = mxDoc.FocusMap;

        ILayer layer;
        for (int i = 0; i < map.LayerCount;i++ )
        {
            layer = map.Layer[i];                
            IFeatureClass featureClass = (layer as IFeatureLayer2).FeatureClass;                

            IFields2 fields = featureClass.Fields as IFields2;                
            IField keyField = null;
            IField tempKeyField = null;
            int fieldIndex = fields.FindField("layerField");
            if (fieldIndex >= 0)
            {
                keyField = fields.Field[fieldIndex] as IField2;
                tempKeyField = keyField;
                featureClass.DeleteField(keyField);
                IFieldEdit2 editedKeyField = (IFieldEdit2)tempKeyField;
                editedKeyField.Type_2 = esriFieldType.esriFieldTypeOID;                    
                featureClass.AddField(editedKeyField);
            }

        }
  • 2
    I don't think that's a permitted procedure. You can't have multiple OIDs, and you can't have none, either. As a rule, you shouldn't mess with OIDs at all. The same rule as goes for bears in the wilderness applies -- Leave them be, and they won't destroy you (probably). – Vince May 25 '18 at 1:00
  • 1
    You can't delete an OID field, it's required, neither can you add an OID field to an existing feature class.. why are you trying to remove/recreate the OID field? It is possible that the field is locked, you might need to test the Schema Locks interface resources.arcgis.com/en/help/arcobjects-net/componenthelp/… – Michael Stimson May 25 '18 at 1:20
  • @Michale Stimson, I have a MXD file configured with database hosted in development server. Now when i take MXD file to test env i will run a tool which will ask user a destination database server details and update all layer within MXD file with destination database server details (i.e. database server hosted in test environment). When MXD is moved to test environment, each layer has to be manually updated with its key because after moving MXD layers dont know what thier key's are. I am creating tool which will be executed in test env and all layers will get updated with test env details. – jay May 25 '18 at 2:11
  • A layer is different to a feature class. You're trying to modify the storage of the database not the layer definition. Why do you say that the layers don't know what there key is? What is special about this field? OID field management is automatic, the layer will know what its OID field is as soon as it is updated... unless you have a database type that supports multiple OID fields (possible in enterprise environment but I've not worked with such). Perhaps you lack database permissions to modify the schema.. Esri error codes aren't often descriptive of the root cause. – Michael Stimson May 25 '18 at 2:18
  • Thank you Michael for your comments. I appreciate you taking out time to help me out. I will confirm once again with requirements team about why we need to update keys. – jay May 25 '18 at 2:32

Your Answer

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

Browse other questions tagged or ask your own question.