I am trying to retrieve a SqlHierarchyID data-type in C#, from a DataRow that has been fetched from a SQL Server database. After the database call, if I write the following:
var id = datarow["TreeID"];
And then inspect the value of id using a watch in visual studio, it is holding the correct hierarchy value (and is non-null).
However, any and all of the following throw an InvalidCastException:
SqlHierarchyId id2 = (SqlHierarchyId) id;
SqlHierarchyId id3 = datarow.Field<SqlHierarchyId>("TreeID");
SqlHierarchyId id4 = (SqlHierarchyId)datarow["TreeID"];
Moreover, asking reflection for the assign-ability, of the two objects:
//returns false:
SqlHierarchyId id5 = new SqlHierarchyId();
return id5.GetType().IsAssignableFrom(id.GetType());
Moreover, trying a "soft cast" returns null:
SqlHierarchyId id6 = id as SqlHierarchyId;
- Trying everything as a SqlHierarchyId? data-type is fruitless.
- I am referencing the 32 bit assembly, and have tried compiling my solution in both 64 and 32 bit modes.
- Initializing an empty SqlHierarchyId instance and then assigning it does not help.
Thanks for any help!
-JT
EDIT: The assembly comes from SQL Server Express 2012 (x86) SDK directory.