transition a null value in database into a code

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

    transition a null value in database into a code

    Hi,

    Imagine that we have a database and one table. Stored procedure returns 3
    columns of table: ID, name, value.
    Name and value can be a null in database. How to map or represent such
    values in .net code? When I map a null value to a string or double variable I
    get an error.

    I have heard that one of solutions can be converting and representing
    db.null as a minimum value of used type. Is it a good technic? And what in
    situaltion when I need all the values variable can represent from minimum to
    maximum?

    Thanks.
    Przemo
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: transition a null value in database into a code

    Przemo,

    If you are using .NET 2.0, you can use the nullable type.

    When you are dealing with your data set though, you can use the return
    value from the Value property on DBNull to indicate null in a database.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Przemo" <Przemo@discuss ions.microsoft. com> wrote in message
    news:70B7186B-69E3-4AF2-851C-E22C3717C503@mi crosoft.com...[color=blue]
    > Hi,
    >
    > Imagine that we have a database and one table. Stored procedure returns 3
    > columns of table: ID, name, value.
    > Name and value can be a null in database. How to map or represent such
    > values in .net code? When I map a null value to a string or double
    > variable I
    > get an error.
    >
    > I have heard that one of solutions can be converting and representing
    > db.null as a minimum value of used type. Is it a good technic? And what in
    > situaltion when I need all the values variable can represent from minimum
    > to
    > maximum?
    >
    > Thanks.
    > Przemo[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: transition a null value in database into a code

      Hi,

      before assigning the value readed from the DB you can check for
      DBNull.Value , if true then you have to decide what to do, possibles options
      include:

      1- In case of values types assign a especific value, like Int32.MinValue,
      Int32.MaxValue, etc
      1.a - In references types, you can always assign null and later check for
      it, or you can use a "default" instance

      2- In case that all the values of a type are possible (like bool) you have
      to define a wrapper, with two members, the value and a flag that indicate if
      the value exist or not

      3- Upgrade to 2.0 that include a nullable type


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Przemo" <Przemo@discuss ions.microsoft. com> wrote in message
      news:70B7186B-69E3-4AF2-851C-E22C3717C503@mi crosoft.com...[color=blue]
      > Hi,
      >
      > Imagine that we have a database and one table. Stored procedure returns 3
      > columns of table: ID, name, value.
      > Name and value can be a null in database. How to map or represent such
      > values in .net code? When I map a null value to a string or double
      > variable I
      > get an error.
      >
      > I have heard that one of solutions can be converting and representing
      > db.null as a minimum value of used type. Is it a good technic? And what in
      > situaltion when I need all the values variable can represent from minimum
      > to
      > maximum?
      >
      > Thanks.
      > Przemo[/color]


      Comment

      • sdurity@cornercap.com

        #4
        Re: transition a null value in database into a code

        Another option to consider is having your database do some of the work
        for you. You may have a SQL command like NVL, that will return the
        value for the column if it isn't null or some default value if it is
        null.

        Example:
        SELECT fname, NVL (addr, 'Address unknown') AS address
        FROM employees

        (This is the Informix NVL syntax)

        Comment

        Working...