I encountered an error saying that “Unable to cast object of type ‘System.Int16
‘ to type ‘System.Int32
‘.” Isn’t it too unlogical? How come? When I try the following on immediate windows, the output as such that
reader.GetInt16(mycolumn_ordinal_value)
999
reader.GetInt32(mycolumn_ordinal_value)
Exception thrown: 'System.InvalidCastException' in Microsoft.Data.SqlClient.dll
At DB level (sqlserver), it is defined as mycolumn smallint
.
When I ask it to Github Copilot it says literally,
However, when you try to use
reader.GetInt32(mycolumn)
, it throws a
System.InvalidCastException
. This is unusual(you can’t be serious bruh!!) becauseGetInt32
should be able to handle any value thatGetInt16
can handle, since
the range of a 32-bit integer (-2,147,483,648
to2,147,483,647
) is
much larger.
I don’t understand it. Any idea?