PC Review


Reply
Thread Tools Rate Thread

When is 19 Not an Integer

 
 
Jonathan Wood
Guest
Posts: n/a
 
      14th Aug 2008
Er... When it's a Decimal.

I'm executing a query that includes the following: "INSERT INTO [...];
SELECT SCOPE_IDENTITY()"

I then attempt to retrieve the scope identity with code that looks like
this:

int contractId = (int)cmd.ExecuteScalar();

I get an "Invalid cast" error. Examing the value returned by
ExecuteScalar(), I see it is 19 and of type Decimal.

Can anyone explain to me why a Decimal with a value of 19 cannot be cast to
an integer?

Thanks.

Jonathan

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      14th Aug 2008
You can't cast anything to an integer if it is not an integer. But you can
convert:

int contractId = System.Convert.ToInt32(cmd.ExecuteScalar());

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:%23fYD$0h$(E-Mail Removed)...
> Er... When it's a Decimal.
>
> I'm executing a query that includes the following: "INSERT INTO [...];
> SELECT SCOPE_IDENTITY()"
>
> I then attempt to retrieve the scope identity with code that looks like
> this:
>
> int contractId = (int)cmd.ExecuteScalar();
>
> I get an "Invalid cast" error. Examing the value returned by
> ExecuteScalar(), I see it is 19 and of type Decimal.
>
> Can anyone explain to me why a Decimal with a value of 19 cannot be cast
> to an integer?
>
> Thanks.
>
> Jonathan
>



 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      14th Aug 2008
Jonathan Wood has brought this to us :
> Er... When it's a Decimal.
>
> I'm executing a query that includes the following: "INSERT INTO [...]; SELECT
> SCOPE_IDENTITY()"
>
> I then attempt to retrieve the scope identity with code that looks like this:
>
> int contractId = (int)cmd.ExecuteScalar();
>
> I get an "Invalid cast" error. Examing the value returned by ExecuteScalar(),
> I see it is 19 and of type Decimal.
>
> Can anyone explain to me why a Decimal with a value of 19 cannot be cast to
> an integer?
>
> Thanks.
>
> Jonathan


The value returned by ExecuteScalar is an object, in this case a boxed
decimal. You can only unbox it to the exact type, even though the value
(19) could (now) fit in an integer.

This would work: int contractId = (int)(decimal)cmd.ExecuteScalar();

First unbox the returned decimal and *then* cast it to an integer.

Hans Kesting


 
Reply With Quote
 
Norman Yuan
Guest
Posts: n/a
 
      14th Aug 2008
The real question is why SCOPE_IDENTITY() returns a decimal value.

Usually, when design a table, a column is set as Identity, its data type is
integer, however, you can also set the column's data type as decimal with
scale=0 and still set Identity=True.

So, it must be that the table's Identity column's data type is decimal. Go
check the table's design.

If the column is decimal type, then like other replies pointed out, you
cannot directly cast cmd.ExecuteScalar()'s returning value with
(int)cmd.ExecuteScalar()


"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:%23fYD$0h$(E-Mail Removed)...
> Er... When it's a Decimal.
>
> I'm executing a query that includes the following: "INSERT INTO [...];
> SELECT SCOPE_IDENTITY()"
>
> I then attempt to retrieve the scope identity with code that looks like
> this:
>
> int contractId = (int)cmd.ExecuteScalar();
>
> I get an "Invalid cast" error. Examing the value returned by
> ExecuteScalar(), I see it is 19 and of type Decimal.
>
> Can anyone explain to me why a Decimal with a value of 19 cannot be cast
> to an integer?
>
> Thanks.
>
> Jonathan
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diff bw Small integer and Capitol integer vijjirams@gmail.com Microsoft ASP .NET 0 17th Sep 2008 03:41 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= Microsoft ASP .NET 7 7th Feb 2006 02:30 AM
'AddressOf' expression cannot be converted to 'Integer' because 'Integer'is not a delegate type. Patrick Dugan Microsoft VB .NET 3 14th Mar 2005 03:12 PM
Can't create Integer field, a Long Integer is created instead? cbu Microsoft Access Database Table Design 1 2nd Jan 2004 04:58 PM
division of variable integer into best fit integer parts Eddy Microsoft Excel Discussion 7 13th Sep 2003 07:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:45 PM.