title: Error casting tiny int to int
body: <p>This error looks like it was caused by installing framework 4.5 on the server even though the project is still targeted to 4.0.</p>
  
  <p>4.5 replaces the CLR and it looks like it has changes in unboxing an object of type tinyint to a int. This was working in 4.0 but not after installing 4.5.</p>
  
  <p>============================================</p>
  
  <p>Please read the whole question before answering, most current answers do not answer the question I am asking.</p>
  
  <p>We today got an error in a cast from a tinyint in sql to an int using</p>
  
  <pre><code>Daterow datarow = GetOneDatarow(connection,
                           "SELECT tinyintcolumn FROM table1 WHERE id = 4");
  int i = (int)datarow["tinyintcolumn"];
  </code></pre>
  
  <p>This is old code that has been in the product for several years without any changes and it has been working up until yesterday. (And it is not the exact code, just enough to show the context)</p>
  
  <p>=== UPDATE </p>
  
  <p>The exact error message was: "The specified cast is not valid!" and the last line </p>
  
  <pre><code>int i = (int)datarow["tinyintcolumn"];
  </code></pre>
  
  <p>is the exact row from our code casting the error with only variable names and column name changed.</p>
  
  <p>And the database column was tinyint with default value 0, no indexes or other constraints.</p>
  
  <p>=== End update</p>
  
  <p>=== UPDATE 2</p>
  
  <p>Henk Holterman in his response informed me that FW 4.5 replaces the CLR of 4.0 even for projects compiled specifically for 4.0 and this could remotely change existing 4.0 behaviour just like this.</p>
  
  <p>I will keep this open a while more but his answer is the most promising so far  :D
  === End</p>
  
  <p>We changed from framework 3.5 to 4.0 a few weeks ago but it was only yesterday afternoon after a recompile that this happened, yesterday morning the same code (even after recompile) worked like clockwork.</p>
  
  <p>Do anyone have any idea on why this was working before and is not working now?</p>
  
  <p>Has Microsoft made any under the hood changes that removed an implicit conversion or has it worked by pure magic before?</p>
  
  <p>We solved it by changing the database column to int, but I am still curious on what might have caused it to fail right now.</p>
  
  <p>=== UPDATE 3</p>
  
  <p>Just to complete this.</p>
  
  <p>I found the change between frameworks. In an update Microsoft changed how boxing and unboxing is done. This caused the implicit cast from byte to int that older FW did to fail when the byte was boxed, as it is in a datatable.</p>
  
  <p>An unboxed byte will will in 4.5 be implicitly cast to an int, but a boxed byte is an generic object which cannot be implicitly cast.</p>
  
  <p>Not, this was changed in 3.5 SP1 so our FW 4.0 should also have failed, unless the update to SP1 was not in the 4.0 update. This has yet to be answered :)</p>
  
  <p>Here is the ticket from MS on this ;)
  <a href="https://connect.microsoft.com/VisualStudio/feedback/details/766887/casting-tinyint-from-sql-datarow-to-int-no-longer-possible" rel="noreferrer">https://connect.microsoft.com/VisualStudio/feedback/details/766887/casting-tinyint-from-sql-datarow-to-int-no-longer-possible</a></p>