dealing with many different types of integer!! 
Author Message
 dealing with many different types of integer!!

in SQL there is tinyint, smallint, int, etc.

unfortunately you cannot cast all these types to an integer
when returned from a dataset. even casting from int16 to int32
doesnt seem to work.

i've ended up doing

int userid = int.Parse(datarow["userid "].toString());

instead of

int userid = (int) datarow["userid"];

i dont like to do this? am i missing something obvious or
will i need to parse the int or do some kind of reflection
operation on the variable to determine its type first.

whats happening is there is code like (int) datarow["userid"]
which is fine till somebody demotes the data to a tinyint...

-simon



Sun, 30 Jan 2005 09:57:16 GMT  
 dealing with many different types of integer!!
I'm pretty sure you are missing something, I'm just not sure what :-)  I do
this type of thing all over my code and I never do anything like the Parse
call you mention.  I would suggest that you inspect your datarow carefully
in the de{*filter*}.  Perhaps "userid" is not the type you think it is.


Sun, 30 Jan 2005 20:16:42 GMT  
 dealing with many different types of integer!!
Simon,

    Look in the de{*filter*} at the type returned by:

datarow["userid"]

    What is it?  This should solve your problem (one way or another).

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> in SQL there is tinyint, smallint, int, etc.

> unfortunately you cannot cast all these types to an integer
> when returned from a dataset. even casting from int16 to int32
> doesnt seem to work.

> i've ended up doing

> int userid = int.Parse(datarow["userid "].toString());

> instead of

> int userid = (int) datarow["userid"];

> i dont like to do this? am i missing something obvious or
> will i need to parse the int or do some kind of reflection
> operation on the variable to determine its type first.

> whats happening is there is code like (int) datarow["userid"]
> which is fine till somebody demotes the data to a tinyint...

> -simon



Sun, 30 Jan 2005 21:43:22 GMT  
 dealing with many different types of integer!!
One possibility occured to me.  Is the value DBNull instead of int32?  This
has caught me a few times.


Sun, 30 Jan 2005 22:55:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to deal with large integers?

2. Globals in different files, different types, same names

3. Different value when converting into different type

4. Dealing with 8byte Integers

5. Class dealing with integers base 256

6. How to deal with different OS

7. cast cstring type to integer type

8. Using types in a different assembly given that the type may be used or not used

9. A type that maps to different types on different cpus?

10. Proposal -- new integer types [bitfields - let's free them from unions]

11. Type casting string to integer

12. exact integer range of floating point type

 

 
Powered by phpBB® Forum Software