Delete multiple Fields from FeatureClass

4717
9
05-26-2010 12:07 AM
Marianne_BilstedWiese
New Contributor
Hi Forum

I am trying to remove multiple fields from a feature class, with the following C# code.

The list of fields to be removed is in backward order (highest index first), and I do not remove required fields.

However, I can only remove 1 field each time I run this program.
Second call to featureClass.DeleteField(IField field) throws an AccessViolationException.

Am I missing some sort of 'update feature' between calls??

Any suggestions will be appreciated.

        private static void deleteFields(IFeatureClass featureClass)
        {
            ISchemaLock schemaLock = featureClass as ISchemaLock;

            try
            {
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

                deleteField(featureClass, "ANGLE");
                deleteField(featureClass, "SCALE");
                deleteField(featureClass, "POLYGONID");
                deleteField(featureClass, "GM_S_ARG");
                deleteField(featureClass, "GM_I_ARG");
                deleteField(featureClass, "GM_FEATURE");
                deleteField(featureClass, "GM_CATEGORY");
                deleteField(featureClass, "GM_VALID_ELEVATI");
                deleteField(featureClass, "GM_SOURCE_T_SCAL");
                deleteField(featureClass, "GEOPOINT_ID");
                deleteField(featureClass, "GEOPOINT");
                deleteField(featureClass, "PERIMETER");
                deleteField(featureClass, "AREA");

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
            }

        }

        private static void deleteField(IFeatureClass featureClass, String fieldName)
        {

            int index = featureClass.FindField(fieldName);
            IFields fields = featureClass.Fields;
            IField field = null;

            if (index != -1)
            {
                field = fields.get_Field(index);
                featureClass.DeleteField(field);<-----AccessViolationException (not at first call)
            }
            else
                Console.WriteLine(fieldName + " not found");
        }
0 Kudos
9 Replies
NeilClemmons
Regular Contributor III
What is the code behind deleteField?  I don't see anything wrong with your code and I'm pretty sure I've deleted multiple fields at the same time before.
0 Kudos
Marianne_BilstedWiese
New Contributor
Hi Neil

Can't you see the code in deleteField() if you scroll down?

Thanks for the answer anyway

Marianne
0 Kudos
Marianne_BilstedWiese
New Contributor
If you mean any other code:
It is quite a simple program, with a main() and a method to connect to the geodatabase, nothing else so far.

        [STAThread()]
        static void Main(string[] args)
        {
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcInfo },
            new esriLicenseExtensionCode[] { });

            IWorkspace workspace = connectToFileGDB();
            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
            featureClass = featureWorkspace.OpenFeatureClass("geopoint_point");

            try
            {
                deleteFields(featureClass);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            
            //Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }

        private static IWorkspace connectToFileGDB()
        {
            string nameOfFile = "D:\\Documents and Settings\\mbw\\Skrivebord\\Langsom\\G500_k4_GeoPoint_2.gdb";

            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)new FileGDBWorkspaceFactoryClass();
            IWorkspace workspace = workspaceFactory.OpenFromFile(nameOfFile, 0);

            return workspace;
        }
0 Kudos
NeilClemmons
Regular Contributor III
Wow, that was really strange.  The code window didn't have a scrollbar.  I restarted my computer and voila! there it is.  Your code looks fine to me.  I wonder if its a resource reference that isn't getting cleaned up.  In the deleteField method, try calling Marshal.FinalReleaseComObject on the Field and Fields references you are using and see if that clears it up.
0 Kudos
Marianne_BilstedWiese
New Contributor
Hi Neil

I tried to do as you suggested.

I even tried to make the featureClass a class member and open it from the workspace everytime before deleting a new field. 

I still get AccessViolationException on the second call to deleteField()

Probably I will just delete those fields one by one 😐
I also get an error when using the ArcToolbox->Data Management Tools -> Fields -> Delete Fields
ERROR 999998: Unexpected Error.
Failed to execute (DeleteField).

There might be something wrong with these Feature classes that I have. They were migrated from an ArcInfo system.

Thanks for your help.

Marianne

        private static String inFeatureClassName = "geopoint_point";
        private static IFeatureClass featureClass;

        private static void deleteFields()
        {

            try
            {
                deleteField("ANGLE");
                deleteField("SCALE");
                deleteField("POLYGONID");
                deleteField("GM_S_ARG");
                deleteField("GM_I_ARG");
                deleteField("GM_FEATURE");
                deleteField("GM_CATEGORY");
                deleteField("GM_VALID_ELEVATI");
                deleteField("GM_SOURCE_T_SCAL");
                deleteField("GEOPOINT_ID");
                deleteField("GEOPOINT_");
                deleteField("PERIMETER");
                deleteField("AREA");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        // Something wrong, Cashes with AccesViolationException 
        private static void deleteField(String fieldName)
        {
            refreshFeatureClass();

            int index = featureClass.FindField(fieldName);
            
            IFields fields = featureClass.Fields;
            IField field;

            if (index != -1)
            {
                field = fields.get_Field(index);

                try
                {
                    featureClass.DeleteField(field);

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Marshal.FinalReleaseComObject(field);
            }
            else
                Console.WriteLine(fieldName + " not found");

            Marshal.FinalReleaseComObject(fields);

        }

        private static void refreshFeatureClass()
        {
            
            IFeatureWorkspace featureWorkspace = featureClass.FeatureDataset.Workspace as IFeatureWorkspace;

            Marshal.FinalReleaseComObject(featureClass);

            featureClass = featureWorkspace.OpenFeatureClass(inFeatureClassName);

        }

0 Kudos
KirkKuykendall
Occasional Contributor III
On some systems "Scale" is not a legal field name ... try commenting out that line.
0 Kudos
Marianne_BilstedWiese
New Contributor
Hi Kirk

The "SCALE" fieldname can't be the problem here.
I get the same error even if i deleted the scale field beforehand, and I comment out deleteField("SCALE");
AccessViolationException
Message = "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."


Also, the first time I run the program it will delete the ANGLE - field and fail on SCALE-field.
The next time I run the program it will tell me there is no ANGLE-field, delete the SCALE-field and fail on POLYGONID-field. And so on...

For now, I will not use more time on this. (The next step would be to test the program on some other featureClass or to close and open the workspace between calls to deleteFields, I guess)
I will delete those fields manually, letting my program start its work from there.

Thanks for your try.

Marianne
0 Kudos
AnilDhiman
New Contributor
Hi,
I know it been quite in this thread for a long time. just wanted to know, if you get any solution to this issue. I am also facing a similar scenario.

Anil
0 Kudos
MikeThompson4
New Contributor

First make a list of all the field objects to delete, and then delete them one by one.

0 Kudos