Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Interesting. Does using Marshal.FinalReleaseComObject(fWorkspace) have the same effect? –  blah238 Nov 17 '11 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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