I am using MsSql2008FeatureLayer to display spatial data and I am trying to insure that I have spatial indices on all of my geometry tables. I get an error on one table. It has a single point type feature in it. I have checked that the feature is valid (STIsValid). I get this error:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
The Location field is not null. I realize there is not much need for a spatial index on a table with only one record. Is this not allowed?
Charles
This also happens with a table that contains a single line string.
Charles
Charles,
Thanks for your post.
I agree with you that this problem will be happening when calling the GetBoundingBox for an MsSqlFeatureLayer in which only one feature exists. Following code shows a way to go around this problem now; we will try to fix it in future versions.
private void DisplayMap_Load(object sender, EventArgs e)
{
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
string connectString = "Data Source=192.168.0.212;Initial Catalog=**;Persist Security Info=True;User ID=sa;Password=****";
MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(connectString, "cntry02", "ID");
sql2008Layer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
sql2008Layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay staticOverlay = new LayerOverlay();
staticOverlay.Layers.Add("Sql2008Layer", sql2008Layer);
winformsMap1.Overlays.Add(staticOverlay);
sql2008Layer.Open();
winformsMap1.CurrentExtent = GetSqlLayerBoundingBox(sql2008Layer);//sql2008Layer.GetBoundingBox();
sql2008Layer.Close();
winformsMap1.Refresh();
}
private RectangleShape GetSqlLayerBoundingBox(MsSql2008FeatureLayer sql2008Layer)
{
RectangleShape returnBoundingBox = new RectangleShape();
try
{
sql2008Layer.Open();
int count = sql2008Layer.QueryTools.GetCount();
if (count == 1)
{
returnBoundingBox = sql2008Layer.QueryTools.GetAllFeatures(ReturningColumnsType.NoColumns)[0].GetBoundingBox();
}
else
{
returnBoundingBox = sql2008Layer.GetBoundingBox();
}
}
finally
{
sql2008Layer.Close();
}
return returnBoundingBox;
}
Any more questions just feel free to let me know.
Thanks.
Yale