I was running into a problem trying to convert an SQL Server smallint column type to an int in my DAL Provider class. Here is a snippet of my code that was failing (the names are different but the pattern is the same as outlined in the book):
Code:
protected virtual SupplementalSheetPlayerDetails GetSupplementalSheetPlayerFromReader(IDataReader reader) {
SupplementalSheetPlayerDetails supplementalSheetPlayer = new SupplementalSheetPlayerDetails((int)reader["SupplementalSheetID"],(int)reader["PlayerID"],(int)reader["Seqno"]);
return supplementalSheetPlayer;
}
In my database, the 'Seqno' column is declared as a smallint, but when I cast it as an integer it causes a runtime error. I solved the error by instead casting it as a short (even though the constructor for the SupplementalSheetPlayerDetails object accepts an integer for the Seqno property) and that seemed to work, but I'm still not sure why I'm getting a casting error in the first place.
Any ideas on why I can cast a smallint as a short but not an int, and also why the smallint is then implicitly cast to an int in the constructor of the entity object?
Thanks.