G
Guest
When using a DataReader to return records from a SQL server, I'm having a
problem with the following code (abbreviated)
int ID = 0;
ID = (int)dr["user_id"];
will work if the user_id column is an int field. The same code throws an
invalid cast exception if the user_id column is a smallint. To fix the
problem, this code works:
int ID = 0;
ID = (int)(smallint)dr["user_id"];
I can also use:
ID = dr.GetInt16(0);
but that's not as immediately obvious to someone that is looking at my code.
The double cast seems kind of hokey......what's the proper way to make this
assignment?
problem with the following code (abbreviated)
int ID = 0;
ID = (int)dr["user_id"];
will work if the user_id column is an int field. The same code throws an
invalid cast exception if the user_id column is a smallint. To fix the
problem, this code works:
int ID = 0;
ID = (int)(smallint)dr["user_id"];
I can also use:
ID = dr.GetInt16(0);
but that's not as immediately obvious to someone that is looking at my code.
The double cast seems kind of hokey......what's the proper way to make this
assignment?