During a process I am running, I sometimes need to create a field that is missing in an attribute table. Unfortunately, the program is complaining that the table I am adding the field to has a schema lock from the program itself. So I attempted to add the ISchemaLock method for granting an exclusive lock and I now get the HRESULT error 0x8004022D when attempting to change the lock. Here is what the code looks like.

private void DoWork()
        IWorkspace workspace = getWorkspacefromPath(workspacepath);
        IFeatureWorkspace fWorkspace = (IFeatureWorkspace)workspace;
        IFeatureClass fc = fWorkspace.OpenFeatureClass(targetfile);
        if (fc == null)
            MessageBox.Show("No feature class found.", "Error.");
        ITable table = (ITable)fc;
        int index = table.FindField("PSTATE");
        if (index == -1)
            ISchemaLock schemaLock = (ISchemaLock)table;
            settesttext("No PSTATE field found. Adding field now.");


            catch (Exception error)
                MessageBox.Show("Field creation error. Closing process." + "\n " + error.Message);

            settesttext("Field creation complete");
            index = table.FindField("PSTATE");

How should I go about adding the field to avoid getting a schema lock problem?

  • Have you tried using the geoprocessor tool AddField in your code instead? – kenbuja Nov 17 '11 at 18:43
  • No, I have not, but this method has worked in the past. I would like to know the root cause of this error. – Nathanus Nov 17 '11 at 18:48
  • What type of workspace is it (shapefile, file geodatabase, personal geodatabase, SDE)? – blah238 Nov 17 '11 at 19:02
  • It's a file GDB workspace. There is no other work being done on the target feature class when the process takes place. The only interaction with it is shown in the method above. – Nathanus Nov 17 '11 at 19:05
  • Ah... figured it out. Hold on. – Nathanus Nov 17 '11 at 19:13

It appears that the lock was due to the IFeatureWorkspace maintaining a reference to the file GDB I was using.


        fWorkspace = null;

after creating the IFeatureClass instance did the trick. Thanks @blah238 for making me consider the workspace.

  • Interesting. Does using Marshal.FinalReleaseComObject(fWorkspace) have the same effect? – blah238 Nov 17 '11 at 19:23

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.