The SqlGeometry with Microsoft SQL Server

I came across a curious error earlier today when attempting to use a SqlDataReader to read a column of geometry data from a SQL Server table:

System.InvalidCastException: Unable to cast object of type ‘Microsoft.SqlServer.Types.SqlGeometry’ to type ‘Microsoft.SqlServer.Types.SqlGeometry’

SqlGeometry to SqlGeometry… you’d think that would be a pretty easy cast, wouldn’t you? It turns out that this is a problem caused by a conflict between the spatial libraries used in SQL Server Denali compared to that in 2008/R2, and you’ll get this error depending on which version of Microsoft.SqlServer.Types.dll you use, and how you try to access geometry or geography columns from a datareader:

[php]
while (dataReader.Read())
{
// This works in SQL Server 2008/R2, but errors with Denali
SqlGeometry g = (SqlGeometry)dataReader.GetValue(0);

// This works in SQL Server 2008/R2, but errors with Denali
SqlGeometry g = (SqlGeometry)dataReader["GeomCol"];

// This works in Denali, but not in SQL Server 2008/R2
SqlGeometry g = SqlGeometry.Deserialize(reader.GetSqlBytes(0));

// This works in Sql Server 2008/R2/Denali
SqlGeometry g = new SqlGeometry();
g.Read(new BinaryReader(reader.GetSqlBytes(0).Stream));
}
[/php]

After a bit of digging around, it appears that using GetValue or square brackets notation [] to access a geometry/geography field in a SqlDataReader is hard-coded to load the 10.0 (SQL Server 2008) version of the Microsoft.SqlServer.Types library.

If you’ve got side-by-side installations of both SQL Server 2008/R2 and Denali (as I have), and try to reference the 11.0 (Denali) version of Microsoft.SqlServer.Types, you’ll therefore get an assembly mismatch when both versions of the library are loaded, which causes the slightly unhelpful error listed at the top of this post. Even if you’ve only got Denali installed, your code may still try to reference a (non-existent) 2008/R2 version of the Microsoft.SqlServer.Types.dll library, so you’ll get a different error instead:

Could not load file or assembly ‘Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91′ or one of its dependencies. The system cannot find the file specified.

The simplest way to resolve these errors is by changing the way you reference any geography/geometry (and, I imagine, hierarchyid) columns from your DataReader, as in the code example above. Alternatively, you can set up an assembly redirection in the application configuration file as explained here (about halfway down), which will allow you to correctly target the Denali version.

As per the What’s new in SQL Server Denali whitepaper, “… side-by-side installations of SQL Server Code-Named “Denali” CTP1 and CTP3 are not supported with existing SQL Server 2008 installations …”, so perhaps I only have myself to blame for this. Interestingly though, the person who raised this MS Connect issue, says that they have experienced exactly the same problem on a clean install of Denali CTP3. The response from Microsoft suggests that this may be due to older versions of the library being packaged with Visual Studio 2010, and also confirms that the problem will not be resolved prior to RTM of SQL Server Denali.

Strangely, I encountered another curious error a few months ago concerning version conflicts of Microsoft.SqlServer.Types. My CTP3 Management Studio Spatial Results tab does not plot curved geometries (selecting a CircularString or the result of BufferWithCurves etc. just produces a blank pane). I had originally assumed that, since this was only a CTP release, this feature had simply not been added yet. It turns out that curved geometries are supported in SSMS CTP3 Spatial Results tab but, if you have side-by-side SQL Server 2008 and Denali, this can corrupt this feature. I guess the reason is similar – that SSMS is somehow attempting to load the SQL Server 2008/R2 version of Microsoft.SqlServer.Types, which, of course, doesn’t support curved geometries.

Mapping the football searches

Fall means lots of things in the United States: leaves changing colors, apple picking, back to school and…football. This weekend, the National Football League (NFL) starts its season with a bang, with 15 games on Sunday and Monday. College and high school football are already underway and fans all across the country are tuning in, getting excited and rooting for their teams—in person, on television and online. As a Notre Dame alum (class of ‘87), the ups (and the recent downs) of Fighting Irish football are always a big part of my fall weekends. Regardless of your alma mater or hometown team, one thing is certain: American football is a big deal across the United States. Ahead of most of this week’s kickoffs, we thought it would be fun to take a peek at some of the football search trends from around the country.

There’s some debate as to what is more popular: professional (NFL) football or college football. Search queries make it clear that in spite of the [nfl lockout], the pro game and [nfl] is consistently more popular for U.S. searchers than all of [college football].

That doesn’t mean that the college game isn’t extraordinarily popular. Right now, we’re seeing the highest level of search queries for [college football] since 2004. In certain regions, college football appears to be king over even the NFL. The states with the most searches for [college football] are mostly in the southern part of the country, with the notable exception of the rabid fans of the Nebraska Cornhuskers. That’s no wonder—on Saturdays, when the Huskers are playing at home, their Memorial Stadium becomes the third largest “city” in the state.

We can also conduct a little popularity contest among players in college football. The Heisman Trophy, awarded annually to the top college player, is usually correlated to spikes in search query volume. This year, the early favorites for the Heisman are beginning to take shape. Stanford’s quarterback [andrew luck], who opted to play his senior year in college instead of heading to the pros, is out front in terms of searches, but fans should also watch out for Robert Griffin III from the Baylor Bears program. [rg3], as he’s known, had the game of his career in week one and might be the national breakout star of the first few weeks of the season.

Over on the NFL side, it appears that winning the big game has little relation to how often people search for their favorite player. In the past month, the most searched for player in the NFL is [peyton manning] of the Indianapolis Colts, who last won the Super Bowl in 2007. He’s currently sidelined with a neck injury, so his status is likely driving much of the current search queries. Meanwhile, the quarterbacks from the past two Super Bowl winners, [drew brees] from the New Orleans Saints and [aaron rodgers] of the Green Bay Packers, come in behind a player who isn’t even his team’s starter: Denver Broncos QB and former Florida Gators star [tim tebow].

The old saying goes, “on any given Sunday,” meaning that every team has a chance to win each time they hit the field. The adage manifests itself in typically balanced standings that last throughout the season and into the playoffs. But when it comes to search, we’re not quite so fair and balanced. In fact, there’s a clear favorite, and by this measure they really are “America’s Team”: the [dallas cowboys]. In the U.S., the Cowboys lead all other teams in search query volume, followed by the [chicago bears] and the [green bay packers].

No examination of football search trends would be complete without mentioning Fantasy Football. Millions of fans participate in their own drafts and watch their league’s waiver wires as they serve as coach and general manager for their own fantasy team. The enthusiasm is so fevered that, in the U.S. right now, more people are interested in [fantasy football] than President [obama].

Finally, for spectators, football isn’t just about teams and players. It’s also about the game day food. [Tailgating] searches peak every fall as folks turn to the web to discover new recipes and ideas for pre-game parking lot cookouts. The Super Bowl in February really drives gameday recipe searches, but tailgating staples like [guacamole], [wings] and [brats] all rank high in terms of search quantity every fall, with the king of all tailgating recipe-related searches being [dip].

Whether you’re an NFL fan, an NCAA nut or just someone who likes hot wings, here’s to a great season.

The HealthGrades website and Bing Maps

 

Named one of the Top 50 Websites of 2011 by TIME, HealthGrades was already the leading online tool to help people rate and find doctors. But HealthGrades was determined to give customers an even better experience. Once users found “Dr. Right,” HealthGrades wanted to give folks an easy way to locate offices and quickly get directions. With Bing Maps, HealthGrades found they could seamlessly integrate maps into their physician search tool. Using the handy Bing Maps developer tools, HealthGrades built the maps integration themselves, with some architecture and design support from development partner, OnTerra. Way to go, HealthGrades!

HealthGrades

Whatever business you’re in, chances are there’s a Bing Maps solution waiting just for you. Build it yourself with our free web or phone SDKs. Or get a little help from one of our Bing Maps partners.