Hi,
I have a button with some code behind it that edits the attributes of selected features when run. I am having an intermittent issue where sometimes the code runs and works properly, and other times it runs and only seems to partially complete. I don't get any error messages in either case, and in each case my code gets all the way to the end and displays a MessageBox with the results. But, when it doesn't work I can see that the attributes on the selected features have not been updated and the process is also not available to 'roll-back' via the undo button (ie. not on the undo-stack).
I have posted the code below, but was just wondering if anyone else has seen this issue. When I notice that the code hasn't worked, I just hit the button again (without changing anything related to my selection in the map) and then it all works fine. I just don't know why it won't work that first time.
Also, this is intermittent. Sometimes it works the first time, sometimes it doesn't. I cannot find any pattern.
protected async override void OnClick()
{
try
{
int updatedFeatures = 0;
await QueuedTask.Run(() =>
{
var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
editOperation.Name = "Update Fittings Diameters on Selected Features";
editOperation.EditOperationType = ArcGIS.Desktop.Editing.EditOperationType.Long;
Map map = MapView.Active.Map;
FeatureLayer waterMainLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterMain").FirstOrDefault();
FeatureLayer waterServiceLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterService").FirstOrDefault();
FeatureLayer hydrantLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Hydrant").FirstOrDefault();
FeatureLayer controlValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ControlValve").FirstOrDefault();
FeatureLayer waterFittingsLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Fitting").FirstOrDefault();
FeatureLayer reliefValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ReliefValve").FirstOrDefault();
FeatureLayer pressureZoneLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_PressureZone").FirstOrDefault();
#region Check for Relief Valves
if (reliefValveLayer != null)
{
if (reliefValveLayer.SelectionCount > 0)
{
var reliefValvesSelection = reliefValveLayer.GetSelection();
IReadOnlyList<long> selectedOIDs = reliefValvesSelection.GetObjectIDs();
var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();
foreach (var oid in selectedOIDs)
{
insp.Load(reliefValveLayer, oid);
MapPoint reliefValvePoint = (MapPoint)insp.Shape;
SpatialQueryFilter wsFilter = new SpatialQueryFilter();
wsFilter.FilterGeometry = reliefValvePoint;
wsFilter.SpatialRelationship = SpatialRelationship.Intersects;
wsFilter.SubFields = "*";
RowCursor wsCursor = waterServiceLayer.Search(wsFilter);
Feature wsFeature;
wsCursor.MoveNext();
wsFeature = (Feature)wsCursor.Current;
if (wsFeature != null)
{
insp["Diameter"] = wsFeature["Diameter"];
insp["WATERMAINID"] = wsFeature["FACILITYID"];
if (Properties.Settings.Default.fitContractNumberSetting == true)
insp["CONTRACTNO"] = wsFeature["CONTRACTNO"];
if (Properties.Settings.Default.fitInstallDateSetting == true)
insp["INSTALLDATE"] = wsFeature["INSTALLDATE"];
if (Properties.Settings.Default.fitDataSourceSetting == true)
insp["DATASOURCE"] = wsFeature["DATASOURCE"];
if (Properties.Settings.Default.fitLifecycleSetting == true)
insp["LifecycleStatus"] = wsFeature["LifecycleStatus"];
}
editOperation.Modify(insp);
updatedFeatures++;
}
}
}
#endregion
editOperation.Execute();
});
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(updatedFeatures.ToString() + " features updated.", "Fittings Tool - Complete");
}
catch
{
}
finally
{
}
}