Hello,
Current software is ArcGIS 10.8.2 on Windows 10, application development with Visual Studio 2022.
I wrote a simple application for a one-time adaptation to a file geodatabase with feature linked annotations, to correct feature and annotation fields and positions. But it fails on IFeatureClass.Update() with the error message 'Objects in this class cannot be updated outside an edit session', although an IWorkspaceEdit is used. It doesn't matter if the fgdb is on a network share or locally stored.
I do not need any graphical interface, so I can't test it with IMap, IMxApplication and IEditor.
Both featureclass and annotationclasses are in a the same featuredataset. I included only the featureclass in the code fragment.
IFeatureWorkspace editableWorkspace;
string workspacePath = "c:\anyfolder\mygeodatabase.gdb";
using (ComReleaser comReleaser = new ComReleaser())
{
object obj = new object();
Type t = null;
comReleaser.ManageLifetime(obj);
t = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
obj = Activator.CreateInstance(t);
IWorkspaceFactory workspaceFactory = obj as FileGDBWorkspaceFactory;
if (workspaceFactory != null)
{
editableWorkspace = workspaceFactory.OpenFromFile(workspacePath, 0) as IFeatureWorkspace;
}
}
IFeatureDataset editableDataset = editableWorkspace.OpenFeatureDataset("mydataset");
IFeatureClass editableFeatureClass;
string featureClassName = "myfeatureclass";
if (editableDataset != null)
{
IEnumDataset enumDataset = editableDataset.Subsets;
enumDataset.Reset();
IDataset currentDataset = enumDataset.Next();
while (currentDataset != null)
{
if (currentDataset is IFeatureClass && currentDataset.Name == featureClassName)
{
editableFeatureClass = currentDataset as IFeatureClass;
break;
}
currentDataset = enumDataset.Next();
}
}
IWorkspaceEdit2 workspaceEdit;
workspaceEdit = editableWorkspace as IWorkspaceEdit2;
workspaceEdit.StartEditing(false);
if (workspaceEdit.IsBeingEdited())
{
workspaceEdit.StartEditOperation();
if (workspaceEdit.IsInEditOperation)
{
IQueryFilter filter = new QueryFilter { WhereClause = "OBJECTID = 1" };
IFeatureCursor cursor = editableFeatureClass.Update(filter, false); // throws error
if (cursor != null)
{
IFeature feature = cursor.NextFeature();
if (feature != null)
{
feature.set_Value(3, "mytext");
feature.Store();
}
}
Marshal.ReleaseComObject(cursor);
workspaceEdit.StopEditOperation();
}
workspaceEdit.StopEditing(true);
}
I can read the fgdb in C# and also edit it in ArcMap. Must doing something wrong, but can't see it.
Many thank for helping,
Kind regards,
Pieter
It's been a long time and I may be wrong, but I think IFeatureClass.Update will not work on a versioned dataset.
You should be able to simply create a normal cursor on your QueryFilter. The rest of the code should do your editing properly.
There is a chart on this help document:
https://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#IFeatureClass_Search.htm
that explains it more.
Good Luck
Brent Hoskisson
As far as I know the datasets in this file geodatabase are not versioned. When I try to 'register as versioned' one of the datasets, I get this error:
000133 : The dataset cannot be versioned.
There could be a schema lock on the dataset. The dataset must be registered with the geodatabase. The dataset cannot support versioning.
The file geodatabase was created in 2012 with ArcGIS and migrated to 10 in time. There was and is no need for versioning.
I did a lot of editing other file geodatabases the same way (with IWorkspaceEdit2) without any problem, but this particular one refuses.
P.S. you might want to put your Cursor.NextFeature in a while loop to edit all of the cursor items.
IFeature feature;
while ((feature = cursor.NextFeature()) != null)
{
...
}
Brent Hoskisson