Valid SQLGeometry object causing "not valid" exception" when running STNumGeometries() on it

1.2k views Asked by At

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.

0

There are 0 answers