I have a column in SQL Server of smallint type. I read the row including this field by Ado.net.
When I get DataRow of that SQL Server row, I try to convert it to integer like the following code:
int value = datarow.Field<int>("myColumnName");
Because the field is smallint, I think that I should be converted to int. But it throws an exception saying that
Specified cast is not valid…..
I don’t want to use Convert.ToInt32 or other function. I want to use the field extension methods of Datarow. How can I do that?
EDIT:
datarow.Field<int>("myColumnName"); // doesnt work
datarow.Field<Int16>("myColumnName"); // doesnt work
datarow.Field<short>("myColumnName"); // doesnt work
Convert.ToInt32(datarow["myColumnName"]) // works but i dont want it
See SQL Server Data Type Mappings (ADO.NET).
SMALLINTis a 16-bit integer in SQL Server, so it maps toInt16in .NETTherefore, you should use
to read the value