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?

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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.