SQL Server Spatial Data - DataReader.GetFieldType returned null

Programming C# SQL

Let's say we have a table that contains spartial data like geography or geometry and we simply want to query those table via DataAdapter.Fill() then we will run into this exception (note this exception is not of type SqlException but of type System.Exception):

DataReader.GetFieldType(x) returned null. Exception data: System.Collections.ListDictionaryInternal

To solve this, add NuGet package Microsoft.SqlServer.Types to your project.

Next, add this AssemblyBinding to your app.config file:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="" newVersion="" />

Then expand your References folder, select Microsoft.SqlServer.Types and set CopyLocal to true.