After upgrading my ASP.NET webforms application to .NET 4.7.2 (and 4.8) and changing the underlying database server to SQL Server 2017, I started to get this exception:
Exception Details: System.InvalidOperationException: DataReader.GetFieldType(13) returned null.
Underlying datatype of column 13 in the dataset is GEOGRAPHY
. I have researched a bit about possible causes and this are things I have tried without success.
The machine should have an instance of SQL Server installed. I have it. On my dev computer I have SQL Server 2017 Express and the “production” has SQL Server 2017 for the big boys. So, this is not the culprit.
There are suggestions to install package
Microsoft.SqlServer.Types
. I have installed version 14.0.1016.290. The reference was created,Microsoft.SqlServer.Types.dll
was copied in the bin folder. This has not resolved the error.Another suggestion was to put this call into the
Global.asax
:public class Global_asax : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { ... /* For ASP.NET Web Applications, add the following line of code to the Application_Start method in Global.asax.cs: */ SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin")); } }
And as a last resort I have added assembly redirect in a
web.config
. I am not sure if I got publicKeyToken right.<assemblyBinding> ... <dependentAssembly> <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" /> <bindingRedirect oldVersion="10.0.0.0" newVersion="14.0.0.0" /> </dependentAssembly> </assemblyBinding>
None of the above resolved my problem and this were all the suggestions I could find on various forums and stack exchange. I have looked into the source code of GetFieldType()
method found at http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/Data/System/Data/SqlClient/SqlDataReader@cs/1305376/SqlDataReader@cs but it offered no clue.
I have thought about there might be some old remnant from previous .Net version that calls incorrect version of this method, but I do not know how to check this. I just think it is a shame Microsoft cannot keep their stuff together. Old version of my app just simply worked.
IMPORTANT: To all of you who think this question was already answered: it was not! It is a persistent problem.
UPDATE - ADDITIONAL INFORMATION My previous configuration on development computer and production server was .NET 4.0 and SQL Server 2008. After upgrading the database to SQL Server 2017 I changed only the connection strings on existing app and it worked. Actually it is still used in production since newer versions od .NET simply do not work.
After that I upgraded my dev machine to .NET 4.8, entity framework to 6.4 and compiled the app. And as result Geography datatype was broken. All steps I have take to remedy this are above. What is wrong here?