title: SqlHierarchyID casting from Datarow
body: <p>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:</p>
  
  <pre><code>var id = datarow["TreeID"];
  </code></pre>
  
  <p>And then inspect the value of id using a watch in visual studio, it is holding the correct hierarchy value (and is non-null).</p>
  
  <p>However, any and all of the following throw an InvalidCastException:</p>
  
  <pre><code>SqlHierarchyId id2 = (SqlHierarchyId) id;
  SqlHierarchyId id3 = datarow.Field&lt;SqlHierarchyId&gt;("TreeID");
  SqlHierarchyId id4 = (SqlHierarchyId)datarow["TreeID"];
  </code></pre>
  
  <p>Moreover, asking reflection for the assign-ability, of the two objects:</p>
  
  <pre><code>//returns false:
  SqlHierarchyId id5 = new SqlHierarchyId();
  return id5.GetType().IsAssignableFrom(id.GetType());
  </code></pre>
  
  <p>Moreover, trying a "soft cast" returns null:</p>
  
  <pre><code>SqlHierarchyId id6 = id as SqlHierarchyId;
  </code></pre>
  
  <ul>
  <li>Trying everything as a SqlHierarchyId? data-type is fruitless.</li>
  <li>I am referencing the 32 bit assembly, and have tried compiling my solution in both 64 and 32 bit modes.</li>
  <li>Initializing an empty SqlHierarchyId instance and then assigning it does not help.</li>
  </ul>
  
  <p>Thanks for any help!</p>
  
  <p>-JT</p>
  
  <p><strong>EDIT:</strong> The assembly comes from SQL Server Express 2012 (x86) SDK directory.</p>