2

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.");
            return;
        }
        ITable table = (ITable)fc;
        int index = table.FindField("PSTATE");
        if (index == -1)
        {
            ISchemaLock schemaLock = (ISchemaLock)table;
            settesttext("No PSTATE field found. Adding field now.");
            try
            {

                schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
                table.AddField(fielddata());

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

            }
            finally
            {
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
            }
            settesttext("Field creation complete");
            index = table.FindField("PSTATE");
}

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

5
  • Have you tried using the geoprocessor tool AddField in your code instead?
    – kenbuja
    Commented Nov 17, 2011 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
    Commented Nov 17, 2011 at 18:48
  • What type of workspace is it (shapefile, file geodatabase, personal geodatabase, SDE)?
    – blah238
    Commented Nov 17, 2011 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
    Commented Nov 17, 2011 at 19:05
  • Ah... figured it out. Hold on.
    – Nathanus
    Commented Nov 17, 2011 at 19:13

1 Answer 1

3

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

Adding...

        fWorkspace = null;
        GC.Collect();

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

1
  • Interesting. Does using Marshal.FinalReleaseComObject(fWorkspace) have the same effect?
    – blah238
    Commented Nov 17, 2011 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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