AnsweredAssumed Answered

Getting an "Operation is not valid due to the current state of the object" error. What am I missing?

Question asked by ufshall on Jun 2, 2017
Latest reply on Jun 2, 2017 by cmacleod-esristaff

I am writing a task that will copy the features of a feature class to an SDE feature class. I can compile the program and complete the task without error until I actually try to run the Create command on the edit operation. Once I add that line I receive an AggregateException error stating:

 

"Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away."

 

Looking online, I find solutions telling you to uncheck the Optimize Code option in the Build properties and making sure the "Suppress JIT optimization" is checked in the Debug Options. I have tried both these options, and have cleaned and rebuilt the solution to no avail. What am I missing?

 

This is the code I'm working with:

 

private async Task ProcessParcelFileIntoSdeTask()
{
CountyNameLists nameLists = new CountyNameLists();
string message = string.Empty;
bool processResult = false;
var apnField = cmbAPN.SelectedValue.ToString();
var ownerField = cmbOwner.SelectedValue.ToString();
var selectFips = nameLists.CountyFipsCode(cmbStateList.SelectedValue.ToString(),
cmbCountyList.SelectedValue.ToString());

await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase =
new Geodatabase(new DatabaseConnectionFile(new Uri(@"PARCEL.sde"))))
using (FeatureClass masterParcels = geodatabase.OpenDataset<FeatureClass>("Parcels.DBO.Parcel_USA"))
using (Shapefile shapefile = new Shapefile(new ShapefileConnectionPath(new Uri(ParcelPath))))
{
FeatureClass parcelShapes = shapefile.OpenTable(ParcelFile) as FeatureClass;

EditOperation editOperation = new EditOperation();
editOperation.ProgressMessage = "Inserting parcels...";
editOperation.Callback(context =>
{
try
{
if (parcelShapes != null)
using (RowCursor rowCursor = parcelShapes.Search(null, false))
{
while (rowCursor.MoveNext())
{
Feature feature = rowCursor.Current as Feature;
var attributes = new Dictionary<string, object>();
object polygon = feature.GetShape();

int field = feature.FindField(apnField);
var fips = feature.GetOriginalValue(field);
field = feature.FindField(ownerField);
var owner = feature.GetOriginalValue(field);

attributes["SHAPE"] = polygon;
attributes["FIPS"] = selectFips;
attributes["APN"] = fips;
attributes["OWNER"] = owner;

//PROGRAM CRASHES HERE. WHY?
editOperation.Create(masterParcels, attributes);
}
}

MessageBox.Show("Completed", "Task Finished", MessageBoxButton.OK,
MessageBoxImage.Exclamation);
}
catch (GeodatabaseException exObj)
{
message = exObj.Message;
}
}, masterParcels);

var task = editOperation.ExecuteAsync();
processResult = task.Result;

if (!processResult)
message = editOperation.ErrorMessage;
}
});

if (!processResult)
MessageBox.Show(message, "Parcel Insert Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

Outcomes