title: makeValid() error sql geospatial
body: <p>I'm writing a project in C# where i use ADO.NET to connect to my SQL Server 2012 database. My database (among other things ) has two columns. the first column represent latitude of a point and the second one represents the longitude. I use a procedure that check if two rectangles on the map intercept each the other. The column reply returns 1 value that is either 0 or 1.
  edit: in this example i run it only with numbers, i plan to add variables later
  my procedure works fine if i run it on SQL query. But drops error if i run using ADO.net</p>
  
  <p>as SQL query procedure is :</p>
  
  <pre><code>DECLARE @g geography;
  DECLARE @h geography;
  DECLARE @s geography;
  
  SET @g = geography::STGeomFromText('POLYGON((39.692 23.483, 23.483 39.671, 24.095 39.493, 23.466 39.800,39.692 23.483))', 4326);
  
  SET @h = geography::STGeomFromText('POLYGON((39.800096 23.296509, 39.628961 23.128967,39.43195 23.510742 ,39.7093 23.859558,39.800096 23.296509))', 4326);
  SET @h =@h.MakeValid();
  SET @g = @g.MakeValid();
  SELECT @g.STIntersects(@h) as reply
  </code></pre>
  
  <p>/*==============================================================================================================</p>
  
  <p>//------------------------ AS ADO.NET procedure is : -------------------------------</p>
  
  <pre><code>SqlDataReader rdr = null;
  SqlConnection conn = new SqlConnection("Data Source=AGIS-PC;Initial Catalog=ship_maps;Integrated Security=True");// create a connection object
  
  
  String commandString = @"DECLARE @g geography;
  DECLARE @h geography;
  DECLARE @s geography;
  SET @h =@h.MakeValid();
  SET @g = @g.MakeValid();
  SET @g = geography::STGeomFromText('POLYGON((39.692 23.483, 23.483 39.671, 24.095 39.493, 23.466 39.800,39.692 23.483))', 4326);
  SET @h = geography::STGeomFromText('POLYGON((39.800096 23.296509, 39.628961 23.128967,39.43195 23.510742 ,39.7093 23.859558,39.800096 23.296509))', 4326);
  SET @h =@h.MakeValid();
  SET @g = @g.MakeValid();
  SELECT @g.STIntersects(@h) as reply";
  SqlCommand cmd = new SqlCommand(commandString, conn);
  
  try
  {
  // open the connection
  conn.Open();
  // 1. get an instance of the SqlDataReader
  rdr = cmd.ExecuteReader();
  
  while (rdr.Read())
  {
  // get the results of each column
  string vessel_name = (string)rdr["reply"];
  TextBox1.Text += " " + vessel_name;
  
  // .....
  }//while
  }
  finally
  {
  //...........
  }
  </code></pre>
  
  <p>//=========================================================
  he error is something with makevalid() i guess. I had similar error in the initial query at sql , then i insert makevalid() and it worked.
  edit : it occurs at rdr = cmd.ExecuteReader();</p>
  
  <pre><code>Stack Trace:
  
  
  [SqlException (0x80131904): A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
  System.ArgumentException: 24200: The specified input does not represent a valid geography instance. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a spatial instance to shift slightly.
  System.ArgumentException:
  at Microsoft.SqlServer.Types.SqlGeography..ctor(GeoData g, Int32 srid)
  at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
  .
  A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
  Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation. To create a larger than hemisphere geography instance, upgrade the version of SQL Server and change the database compatibility level to at least 110.
  Microsoft.SqlServer.Types.GLArgumentException:
  at Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIsValid(GeoData&amp; g, Double eccentricity, Boolean forceKatmai)
  at Microsoft.SqlServer.Types.SqlGeography.IsValidExpensive(Boolean forceKatmai)
  at Microsoft.SqlServer.Types.SqlGeography..ctor(GeoData g, Int32 srid)
  at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
  .]
  System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2084358
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5096328
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
  System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2294
  System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
  System.Data.SqlClient.SqlDataReader.get_MetaData() +86
  System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
  System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
  System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
  System.Data.SqlClient.SqlCommand.ExecuteReader() +89
  gmaps.Button11_Click(Object sender, EventArgs e) +185
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 
  </code></pre>
  
  <p>please advise, or if this is for another topic tell me. I'm a beginner with SQL</p>