0

I'm using an SQL Server 2008 database which contains an Geometry column and I'm now having problems loading and manipulating that data in CSharp when it hasn't been a problem before.

I have an object which is of the Microsoft.SqlServer.Types.SqlGeometry type and I need to get the STNumGeometries i.e.:

var numberOfGeometries = tmpDelytas[i].Delyta.DelytaGrans.STNumGeometries();

but it is causing an error:

ArgumentException 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.

I checked the Geometry value in SQLServer and STIsValid there reports that is is valid. (Unsurprising as the code has previously worked).

The geometry is valid according to STIsValid both in CSharp and in SQLServer, and STNumGeometries works in the database and if I put a breakpoint there then the value of STNumGeometries is reported as 1 in the watch. But still the program crashes with this error if I step forward.

Why might my code be suddenly unwilling to process the line of code?

Update I've found a "solution", but would still like to understand the problem because this stinks...

var geomText = tmpDelytas[0].Delyta.DelytaGrans.ToString();
var geom = SqlGeometry.STGeomFromText(new SqlChars(geomText),0);                            
var numberOfGeometries = geom.STNumGeometries();

And this works.

Dave Alger
  • 339
  • 7
  • 23
  • So what's changed? Changed to VS2012? Added/removed project references? – Rikalous Aug 14 '13 at 14:06
  • So far as I can tell, nothing significant has changed. We have added an unrelated class to the project, and we have a version of this code running on a server and nothing has been changed since that publish apart from the addition of a function to an unrelated class. The code on the server works fine against the same test data. – Dave Alger Aug 15 '13 at 08:36
  • Does it complain on all the data points or a subset? – Rikalous Aug 15 '13 at 09:12
  • In this case the polygon is just a circle, a long list of points which hold together fine. There are occassions when there are a number of smaller polygons, but in this test case there's just one object in the array, one delyta class, one geometry in it, which is causing the error. – Dave Alger Aug 15 '13 at 10:06
  • Weird. When it's stopped on the breakpoint, can you drill down to the value in the Locals window and see it there? Just wondering if the fact that you are using the debugger might be an issue? – Rikalous Aug 15 '13 at 10:40
  • When I hit the breakpoint I can access the values no problem, I can put a watch on DelytaGrans.STNumGeometries() and get the expected value of 1. But if I click continue I get exactly the same error. – Dave Alger Aug 15 '13 at 12:26
  • Not sure what to suggest, sorry. – Rikalous Aug 15 '13 at 13:53

0 Answers0