I'm developing in C# using Visual Studio 2013 and SQL Server 2012. I've been able to store polygons in geography columns using T-SQL and am not trying to use the SqlGeography class in code to retrieve the data.
When I try:
SqlGeography polyB = (SqlGeography)dr["extent"]; // stored in OGC Well Known Binary format
to retrieve a polygon from the database I am receiving the message:
An unhandled exception of type 'System.InvalidCastException' occurred in GeoLib.dll
Additional information: [A]Microsoft.SqlServer.Types.SqlGeography cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeography. Type A originates from 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'.
Type B originates from 'Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'.
I suspect a version incompatibility between SQL Server and Visual Studio as indicated by the differing version numbers. Has anyone ever run into this? Maybe I need SQL Server 2014 installed? Any ideas are appreciated!