When is 19 Not an Integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Wood

    When is 19 Not an Integer

    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.Execut eScalar();

    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

  • Eliyahu Goldin

    #2
    Re: When is 19 Not an Integer

    You can't cast anything to an integer if it is not an integer. But you can
    convert:

    int contractId = System.Convert. ToInt32(cmd.Exe cuteScalar());

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]




    "Jonathan Wood" <jwood@softcirc uits.comwrote in message
    news:%23fYD$0h$ IHA.524@TK2MSFT NGP06.phx.gbl.. .
    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.Execut eScalar();
    >
    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
    >

    Comment

    • Hans Kesting

      #3
      Re: When is 19 Not an Integer

      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.Execut eScalar();
      >
      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)c md.ExecuteScala r();

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

      Hans Kesting


      Comment

      • Norman Yuan

        #4
        Re: When is 19 Not an Integer

        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.ExecuteScal ar()'s returning value with
        (int)cmd.Execut eScalar()


        "Jonathan Wood" <jwood@softcirc uits.comwrote in message
        news:%23fYD$0h$ IHA.524@TK2MSFT NGP06.phx.gbl.. .
        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.Execut eScalar();
        >
        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
        >

        Comment

        Working...