Converting DBNull.Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • João Roberto Alonso

    Converting DBNull.Value

    Hi

    I wanto to know if there is a way to convert a DBNull.Value to Decimal and
    returns to me Decimal.Zero?

    Examples

    COLUMN1 = NULL from DB

    Ex1.: cstr((dt.rows(0 ).item("COLUMN1 "))

    returns Error

    Ex2.: dt.rows(0).item ("COLUMN1").toS tring

    returns String.Empty

    Ex3.: cdec((dt.rows(0 ).item("COLUMN1 "))

    returns Error

    Is there a way to return to me Decimal.Zero??

    I use this...

    cdec((iif(dt.ro ws(0).item("COL UMN1") is dbnull.value,
    decimal.zero,dt .rows(0).item(" COLUMN1") ) )








  • William Ryan

    #2
    Re: Converting DBNull.Value

    Your way works, the only other way I can think of is using IsNull(Column1,
    0) in your SQL Statement, which may or may not be the way to go.

    HTH,

    Bill
    "João Roberto Alonso" <alonsojr@kbone t.com.br> wrote in message
    news:udDpQEhfDH A.3024@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi
    >
    > I wanto to know if there is a way to convert a DBNull.Value to Decimal and
    > returns to me Decimal.Zero?
    >
    > Examples
    >
    > COLUMN1 = NULL from DB
    >
    > Ex1.: cstr((dt.rows(0 ).item("COLUMN1 "))
    >
    > returns Error
    >
    > Ex2.: dt.rows(0).item ("COLUMN1").toS tring
    >
    > returns String.Empty
    >
    > Ex3.: cdec((dt.rows(0 ).item("COLUMN1 "))
    >
    > returns Error
    >
    > Is there a way to return to me Decimal.Zero??
    >
    > I use this...
    >
    > cdec((iif(dt.ro ws(0).item("COL UMN1") is dbnull.value,
    > decimal.zero,dt .rows(0).item(" COLUMN1") ) )
    >
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Armin Zingler

      #3
      Re: Converting DBNull.Value

      "João Roberto Alonso" <alonsojr@kbone t.com.br> schrieb[color=blue]
      > Hi
      >
      > I wanto to know if there is a way to convert a DBNull.Value to
      > Decimal and returns to me Decimal.Zero?
      >
      > Examples
      >
      > COLUMN1 = NULL from DB
      >
      > Ex1.: cstr((dt.rows(0 ).item("COLUMN1 "))
      >
      > returns Error
      >
      > Ex2.: dt.rows(0).item ("COLUMN1").toS tring
      >
      > returns String.Empty
      >
      > Ex3.: cdec((dt.rows(0 ).item("COLUMN1 "))
      >
      > returns Error
      >
      > Is there a way to return to me Decimal.Zero??
      >
      > I use this...
      >
      > cdec((iif(dt.ro ws(0).item("COL UMN1") is dbnull.value,
      > decimal.zero,dt .rows(0).item(" COLUMN1") ) )[/color]


      If you don't distinguish between Null and zero, why do you allow Null values
      in the database at all?

      Apart from your current working approach, the only shorter version I can
      think of is a function performing the replacement if necessary:

      Public Function DecimalNull2Zer o(ByVal Value As Object) As Decimal
      If TypeOf Value Is DBNull Then Return 0D
      Return DirectCast(Valu e, Decimal)
      End Function

      Call:
      dim d as decimal
      d = DecimalNull2Zer o(dt.rows(0).it em("COLUMN1"))

      --
      Armin

      Comment

      • steve

        #4
        Re: Converting DBNull.Value

        i don't think there's an option or function in .net that will handle it for
        you. you could create your own to handle various types with an option to
        return a default value like this psuedo code:

        function getDbValue(byva l column as field, byval type as db.datatypes, byval
        default as string) as object
        on error resume next
        if not isnull(column.v alue) return column.value
        return ctype(default, type)
        end function

        *remember the above i *psuedo* code...b4 i get a million flames about the
        details.
        another route would be to overload the above function, removing the "type"
        argument and setting the "default" to int16, int32, long, string, boolean,
        etc. for each type of data you need to deal w/...i think the above would
        save a lot of proc time when resolving which version vb should use and,
        you'd be saving a ton of space in your code project.

        the other option, if you have access and aproval, is to handle it w/n the
        db.

        queries, views, etc. should use:

        sql server: isnull(myColumn , dbo.Zero)
        oracle: nvl(myColumn, 0)

        or in the table def, make the column "DEFAULT 0".

        hth,

        steve


        "João Roberto Alonso" <alonsojr@kbone t.com.br> wrote in message
        news:udDpQEhfDH A.3024@tk2msftn gp13.phx.gbl...[color=blue]
        > Hi
        >
        > I wanto to know if there is a way to convert a DBNull.Value to Decimal and
        > returns to me Decimal.Zero?
        >
        > Examples
        >
        > COLUMN1 = NULL from DB
        >
        > Ex1.: cstr((dt.rows(0 ).item("COLUMN1 "))
        >
        > returns Error
        >
        > Ex2.: dt.rows(0).item ("COLUMN1").toS tring
        >
        > returns String.Empty
        >
        > Ex3.: cdec((dt.rows(0 ).item("COLUMN1 "))
        >
        > returns Error
        >
        > Is there a way to return to me Decimal.Zero??
        >
        > I use this...
        >
        > cdec((iif(dt.ro ws(0).item("COL UMN1") is dbnull.value,
        > decimal.zero,dt .rows(0).item(" COLUMN1") ) )
        >
        >
        >
        >
        >
        >
        >
        >[/color]


        Comment

        Working...