473,480 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 20060

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1416
by: Cybertof | last post by:
Hello, In the code below, is there a shortcut not to use the temp var CurrEmployee ? I would like to use something like "if (Employee)MyArrayList.Name" but don't know the syntax....
17
2930
by: David Sobey | last post by:
hi - I have a class Node, with a child class Num, that inherits from Node. - I have a function "Evaluate" that takes two Nodes as an argument. - I call this function by sending it two "Num"'s....
2
2882
by: rajivpopat | last post by:
I've been reading a discussion thread at http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/119f8362a9f5ff52 regarding typecasting generic collections to...
6
1617
by: kalyan.listsubs | last post by:
Hi, I have the below program which will simply write struct employee to a file (binary mode). The problem here is empid is writen to the file but the name (char name) is not written. I am using...
2
2553
by: srikar | last post by:
Hi all, When I type cast an int value with (void*) I am able to get the value represented in hex format. But in 64 bit I cant convert int value in to (void*) to obtain the value in hex format...
8
2713
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
8
1408
by: buzzweetman | last post by:
Can someone explain this: void Test() { short x = 10; float y = 9.9f; float a = (float)x; a *= y; short b = (short)a; // I get 99. Good!
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.