DataReader.GetFieldType returned null

Rather than changing your project and adding a reference to Microsoft.SQLServer.Types dll you could just put it into the GAC.

In my case I had three versions on my dev machine and added them to the server GAC:

 gacutil.exe -i .\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll
 gacutil.exe -i .\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll
 gacutil.exe -i .\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll

I had other SQL related third party .NET tools on the server that had the same error. After adding the assemblies to the GAC, they all worked fine.

While I don't put my own assemblies into the GAC, I think it is okay to put some Microsoft SQL-Server assemblies there because they affect multiple applications.


Reference the Microsoft.SQLServer.Types dll from the project and for the reference set it as "Copy Local" in the properties of the reference. This will package that DLL up with the website when you deploy it. Then you don't need all of SQL Server installed on your web box in order to use the SQL Server data types. I did this for my website because it was using the geography data type columns and I was getting the same error.


I tried to resolve this issue by adding the Microsoft.SqlServer.Types NuGet package to my project, but that alone did not help. I also had to add the following to the <assemblyBinding> element of my project's App.config:

<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>

The solution that works for me is add "Type System Version=SQL Server 2012" to connection string.

Example: string connectionString = "Data Source=myserver;Initial Catalog=mydatabase;User ID=sa;Password=*******;Type System Version=SQL Server 2012;";