locked
C# Invalid SRID when inserting binary data RRS feed

  • Question

  • Hello , 

    I'm trying to insert some polygons in my DB  using nHibernate and i have received the following error : 

    A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": System.ArgumentException: 24204: The spatial reference identifier (SRID) is not valid. The specified SRID must match one of the supported SRIDs displayed in the sys.spatial_reference_systems catalog view. System.ArgumentException: at Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32 value) at Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader r) at SqlGeography::.DeserializeValidate(IntPtr , Int32 , CClrLobContext* ) . The statement has been terminated.

    but i have entered a SRID = 4326;

    I'm using a

    SqlGeographyBuilder();

    to build a polygon (rectangle) :

                        double left = Math.Min(NELng, SWLng);
                        double right = Math.Max(NELng, SWLng);
    
                        double bottom = Math.Min(NELat, SWLat);
                        double top = Math.Max(NELat, SWLat);
    
                        SqlGeographyBuilder builder = new SqlGeographyBuilder();
    
                        builder.SetSrid(LocationUtils.SRID);
                        builder.BeginGeography(OpenGisGeographyType.Polygon);
                     
                        builder.BeginFigure(top, left);
    
                        builder.AddLine(bottom, left);
                        builder.AddLine(bottom, right);
                        builder.AddLine(top, right);
                        builder.AddLine(top, left);
                        builder.EndFigure();
                        builder.EndGeography();

    what am I doing wrong ? 

    Monday, July 9, 2012 3:13 PM

Answers

  • The latest version of the Entity Framework 5 (currently in Beta) is the only one to provide native support to spatial datatypes. But, these are only available if you're targetting a .NET 4.5 application. There's an example using LINQ at http://msdn.microsoft.com/en-us/hh859721

    If you're using an older version of Linq2SQL or targetting .NET <= 4.0 then you'll have to use a workaround, such as that described at http://blogs.u2u.be/diederik/post/2010/01/03/The-Missing-Linq-to-SQL-Spatial.aspx


    twitter: @alastaira blog: http://alastaira.wordpress.com/

    • Marked as answer by aviatrix Friday, July 13, 2012 7:41 AM
    Tuesday, July 10, 2012 10:22 PM
    Answerer

All replies

  • There's nothing I can see obviously wrong with your code... are you sure that LocationUtils.SRID is an int value 4326?

    twitter: @alastaira blog: http://alastaira.wordpress.com/

    Tuesday, July 10, 2012 7:17 AM
    Answerer
  • My bad , i wasn't using NHibernate , but Linq2Sql .... 

    Can someone show me a sample on how to insert geo data with linq2sql or any other way ?

    Thanks! 

    Tuesday, July 10, 2012 3:51 PM
  • The latest version of the Entity Framework 5 (currently in Beta) is the only one to provide native support to spatial datatypes. But, these are only available if you're targetting a .NET 4.5 application. There's an example using LINQ at http://msdn.microsoft.com/en-us/hh859721

    If you're using an older version of Linq2SQL or targetting .NET <= 4.0 then you'll have to use a workaround, such as that described at http://blogs.u2u.be/diederik/post/2010/01/03/The-Missing-Linq-to-SQL-Spatial.aspx


    twitter: @alastaira blog: http://alastaira.wordpress.com/

    • Marked as answer by aviatrix Friday, July 13, 2012 7:41 AM
    Tuesday, July 10, 2012 10:22 PM
    Answerer