SCOPE_IDENTITY returns decimal from SQL command

Last post 10-31-2007 6:13 PM by swaino. 3 replies.

Sort Posts:

  • SCOPE_IDENTITY returns decimal from SQL command

    10-31-2007, 11:58 AM
    • Member
      198 point Member
    • swaino
    • Member since 10-10-2006, 11:58 AM
    • Posts 540

    Hi,

    I have an sqlCommand return the ID from the last inserted row.  I use SELECT SCOPE_IDENTITY within the SQL.

    When I call the function it returns a Decimal instead of an integer??

    So I want to convert this to an integer, however this is not liked by the compiler since the overloaded method for the decimal conversion is obviously a decimal, but the ExecuteScalar parameter is of type object.

    iReturnVal = decimal.ToInt32(_sqlCommand.ExecuteScalar());  <--- compiler complains....

    So I changed to this:

    iReturnValue =  decimal.ToInt32((decimal)_sqlCommand.ExecuteScalar());

    Can someone tell me if this is the right way to do it? Seems messy code to me...

     

  • Re: SCOPE_IDENTITY returns decimal from SQL command

    10-31-2007, 12:23 PM
    Answer
    • All-Star
      46,513 point All-Star
    • ndinakar
    • Member since 05-05-2003, 12:57 PM
    • Redmond, WA
    • Posts 6,858
    • Moderator
      TrustedFriends-MVPs

    swaino:
    iReturnVal = decimal.ToInt32(_sqlCommand.ExecuteScalar());  <--- compiler complains....

    Decimal.toint32? I thought it was convert.toint32?

    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
  • Re: SCOPE_IDENTITY returns decimal from SQL command

    10-31-2007, 12:25 PM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 5:26 PM
    • West Chicago, IL
    • Posts 2,297

    It should already be an integer.

  • Re: SCOPE_IDENTITY returns decimal from SQL command

    10-31-2007, 6:13 PM
    • Member
      198 point Member
    • swaino
    • Member since 10-10-2006, 11:58 AM
    • Posts 540

     

    Well decimal. provides loads of conversion functions.

    Also ExecuteScalar does return an object not an integer..

Page 1 of 1 (4 items)