471,595 Members | 956 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Casting problem with small int

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?
Jan 14 '06 #1
6 19678

What about something like:

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

?

Jan 14 '06 #2
"Chris Malone" wrote:

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.
Jan 14 '06 #3
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" wrote:
"Chris Malone" wrote:

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.

Jan 14 '06 #4
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)....
Jan 14 '06 #5
Mike <Mi**@discussions.microsoft.com> wrote:
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"];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 14 '06 #6
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.

Jan 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Cybertof | last post: by
17 posts views Thread by David Sobey | last post: by
2 posts views Thread by rajivpopat | last post: by
6 posts views Thread by kalyan.listsubs | last post: by
2 posts views Thread by srikar | last post: by
8 posts views Thread by buzzweetman | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.