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:

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

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