ThinkGeo.com    |     Documentation    |     Premium Support

V14.3.0 - ThrowKeyNotFoundException

ThinkGeo Team,

I’m getting the below ThrowKeyNotFoundException. This only occurs when the application is first started. I’m using a ValueStyle Override for DrawCore. This was first implemented years ago under V10.5, V10.6, and V12.1.0. This has always worked fine and there have been no code changes.

There doesn’t seem to be any issue with the data.

To avoid the Exceptions have recoded as shown below.

I will email you the code and the layer.

I should note that this always occurs once at startup on various features. And it’s only one feature at startup and none of the other 869 Features in the layer. Must be a timing issue.

Thanks,
Dennis

From: TheColumnValue = OneFeature.ColumnValues[this.ColumnName].Trim();

To: bOutCome = OneFeature.ColumnValues.TryGetValue(this.ColumnName, out TheColumnValue);

   2025-03-21 09:42:04,738 ERROR OriStarVision - OriStarThinkGeoClasses->OriStarDefaultValueStyle.DrawCore:
    Unable to OneFeature.ColumnValues.TryGetValue
    LayerName=CookCountyBoundary, FeatureId=19, IsGeometryValid=True, ColumnName=MUNI


2025-03-20 19:22:11,973 ERROR OriStarVision - OriStarVision->UnKnown.FeatureLayer_DrawingException:
LayerName=CookCountyBoundary
Source=System.Private.CoreLib
TargetSite=Void ThrowKeyNotFoundException[T](T)
StackTrace=   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at OriStarThinkGeoClasses.OriStarDefaultValueStyle.DrawCore(IEnumerable`1 TheFeatures, GeoCanvas TheCanvas, Collection`1 TheLabelsInThisLayer, Collection`1 TheLabelsInAllLayers) in C:\OriStarMappingIncThinkGeoUI-V14.3.0\OriStarThinkGeoClasses\OriStarDefaultValueStyle.cs:line 78
   at ThinkGeo.Core.Style.Draw(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ZoomLevel.Draw(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
   at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
   at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
Message=The given key 'MUNI' was not present in the dictionary.

2025-03-20 19:22:11,973 ERROR OriStarVision - OriStarVision->MapOverlay.TheLayerOverlay_DrawingException:
LayerOverlay=StreetsCity
Source=System.Private.CoreLib
TargetSite=Void ThrowKeyNotFoundException[T](T)
StackTrace=   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at OriStarThinkGeoClasses.OriStarDefaultValueStyle.DrawCore(IEnumerable`1 TheFeatures, GeoCanvas TheCanvas, Collection`1 TheLabelsInThisLayer, Collection`1 TheLabelsInAllLayers) in C:\OriStarMappingIncThinkGeoUI-V14.3.0\OriStarThinkGeoClasses\OriStarDefaultValueStyle.cs:line 78
   at ThinkGeo.Core.Style.Draw(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ZoomLevel.Draw(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
   at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
   at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
Message=The given key 'MUNI' was not present in the dictionary.

2025-03-20 19:22:11,982 ERROR OriStarVision - OriStarVision->UnKnown.FeatureLayer_DrawnException:
LayerName=CookCountyBoundary
Source=System.Private.CoreLib
TargetSite=Void ThrowKeyNotFoundException[T](T)
StackTrace=   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at OriStarThinkGeoClasses.OriStarDefaultValueStyle.DrawCore(IEnumerable`1 TheFeatures, GeoCanvas TheCanvas, Collection`1 TheLabelsInThisLayer, Collection`1 TheLabelsInAllLayers) in C:\OriStarMappingIncThinkGeoUI-V14.3.0\OriStarThinkGeoClasses\OriStarDefaultValueStyle.cs:line 78
   at ThinkGeo.Core.Style.Draw(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ZoomLevel.Draw(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
   at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
   at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
   at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
   at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
Message=The given key 'MUNI' was not present in the dictionary.

Hi Dennis,

So when it throws an exception when calling OneFeature.ColumnValues[this.ColumnName].Trim(); what are the items in OneFeature.ColumnValues?, and what is “this.ColumnName” when that happens? And do you mean “this.ColumnName” cannot be found in OneFeature.ColumnValues the first time the map is launched, but can be found later? or it will always throw that exception on one particular feature?

Thanks,
Ben

hi Ben,

There is only one column in OnFeature.ColumnValues and that is “MUNI”.

this.ColumnName is also “MUNI”.

The Exception states:
Message=The given key ‘MUNI’ was not present in the dictionary.

Appears that it is only when the map is launched and not always, but most of the time. I’ll confirm if the issue occurs once the map is fully started.

The feature on which the exception is thrown does vary.

Discovered that a different exception occurs once the map is fully started and the MrSid Layer is toggled between Visible/Hidden.

Message=Collection was modified; enumeration operation may not execute.
Message=Collection was modified; enumeration operation may not execute.

When the Visible property of the MrSid is toggled the Style for the layer in question is changed. If the MrSid is Visible then the layer in question is rendered with features outlined/labeled only. If MrSid is Hidden then the features are rendered with outline/labeled and fill.

The Style is actually changed in real-time when MrSid is changed. This is done for may layers.

Dennis

> 2025-03-21 15:18:35,568 ERROR OriStarVision - OriStarVision->UnKnown.FeatureLayer_DrawingException:
> LayerName=CookCountyBoundary
> Source=System.Private.CoreLib
> TargetSite=Void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
> StackTrace=   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
>    at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
>    at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
> Message=Collection was modified; enumeration operation may not execute.
> 
> 
> 2025-03-21 15:18:35,569 ERROR OriStarVision - OriStarVision->MapOverlay.TheLayerOverlay_DrawingException:
> LayerOverlay=StreetsCity
> Source=System.Private.CoreLib
> TargetSite=Void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
> StackTrace=   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
>    at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
>    at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
> Message=Collection was modified; enumeration operation may not execute.
> 
> 
> 2025-03-21 15:18:35,578 ERROR OriStarVision - OriStarVision->UnKnown.FeatureLayer_DrawnException:
> LayerName=CookCountyBoundary
> Source=System.Private.CoreLib
> TargetSite=Void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
> StackTrace=   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
>    at ThinkGeo.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.mCQ=(GeoCanvas canvas, RectangleShape drawingExtent, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 idsToDraw)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.lyQ=(GeoCanvas canvas, RectangleShape marginWorldExtent, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers, ZoomLevel currentZoomLevel, Collection`1 columnNames, ShapeFileFeatureSource shapeFileFeatureSource, Collection`1 smallBoxes, Collection`1 ids)
>    at ThinkGeo.Core.ShapeFileFeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
>    at ThinkGeo.Core.Layer.ZSA=(GeoCanvas canvas, Collection`1 labelsInAllLayers)
> Message=Collection was modified; enumeration operation may not execute.

Hi Dennis,

For Issue 1, could I understand it this way: It throws the exception “The given key ‘MUNI’ was not present in the dictionary,” but when you check the data, the “MUNI” column does indeed exist in OneFeature.ColumnValues .

I suspect that some asynchronous methods were not properly awaited, leading to simultaneous data loading and map drawing. Could you please verify that all asynchronous methods are properly awaited?

The second issue (“Collection was modified; enumeration operation may not execute.”) could be due to a similar reason. When toggling a layer, there’s a chance that we start to refresh the map before the previous refresh is completed. We should always cancel the previous drawing before the next one begins. A good practice is to have a field variable _cancellationTokenSource , always cancel the token and create a new one before calling RefreshAsync() . Here’s a quick, rough demo; please refer to the cancellation pattern for more details:

_cancellationTokenSource.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
await RefreshAsync(_cancellationTokenSource.Token);

Thanks,
Ben

hi Ben,

I suspected that it was a timing issue, but did not tie it to Async, which certainly seems to be the cause of my issue. There are other odd behaviors in the application that are also most likely related to Async.

I’ve done a little with ThinkGeo async in a couple of my test-bed applications and it has changed the coding paradigm that I use, which is going to require a fair amount of effort to apply to my main applications.

I’m not familiar with a cancellation token but will look into your suggestion of using it.

Appreciate your insight,
Dennis

No problem, Dennis!

Yes, it does take some efforts to make the project go better with async programming. I think you will find it worth it though as everything is more responsive and the programmer in fact has better control about the rendering process. I recommended you just ask Chat GPT about that cancellation patten and ask for some sample code, it make it much easier to understand.

Thanks,
Ben