Casting problem with small int

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?
 
G

Guest

Chris Malone said:
What about something like:

int ID = Int32.Parse(dr["user_id"]);

?
Int32.Parse() is expecting a string parameter, dr["user_id"] is of type
'object'. I can use:

Int32.Parse(dr["user_id"].ToString())

but this seems to be a pretty complex conversion to cast a 'smallint' to an
'int'.

Surely there's a better way.
 
G

Guest

Mike,
look over the documentation for the DataReader class. It has many methods
such as GetInteger, etc.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Mike said:
Chris Malone said:
What about something like:

int ID = Int32.Parse(dr["user_id"]);

?
Int32.Parse() is expecting a string parameter, dr["user_id"] is of type
'object'. I can use:

Int32.Parse(dr["user_id"].ToString())

but this seems to be a pretty complex conversion to cast a 'smallint' to an
'int'.

Surely there's a better way.
 
G

Guest

Thanks, guess I'm going with:

ID = (int)dr.GetInt16(0);

or

ID = dr["user_id"] is DBNull ? 0 : (int)dr.GetInt16(0);

Still seems like I shouldn't have to explicitly cast and Int16 to an Int32
(int)....
 
J

Jon Skeet [C# MVP]

Mike said:
Thanks, guess I'm going with:

ID = (int)dr.GetInt16(0);

or

ID = dr["user_id"] is DBNull ? 0 : (int)dr.GetInt16(0);

Still seems like I shouldn't have to explicitly cast and Int16 to an Int32
(int)....

You don't need to cast if you call dr.GetInt16(0), because that returns
a short which is implicitly convertible to int. You only need to cast
to short if you just use the indexer. That's because it's actually
doing unboxing, which requires exactly the right type (modulo enums).
You should be able to do:

ID = (short) dr["user_id"];
 
G

Guest

Thanks for the help guys, the problem wasn't with the cast, but trying to use
the ternary operator:

ID = dr["user_id"] is DBNull ? 0 : (short)dr["user_id"];

This does not work becuase you must be able to convert both sides of the :
operator. So this...

ID = dr["user_id"] is DBNull ? (short)0 : (short)dr["user_id"];

....does work since both sides of the : operator are of type 'short'. You
learn something new every day. Thanks for steering me in the right direction
guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top