NULLs from SQL--does this make sense?

Discussion in 'ASP .Net' started by Phil Sandler, Aug 19, 2004.

  1. Phil Sandler

    Phil Sandler Guest

    All,

    I come across a lot of instances where a nullable field in my database
    needs to be handled in the application, and I just want to set them to
    an empty value. For example, an integer would be returned as 0, a
    string would be returned as "".

    I know how to do this from the database side (COALESCE), but I need to
    handle it on the application side as well, for cases when I don't have
    control of every tier.

    This is what I've come up with. I'm trying to figure out cases where
    it might not work.

    -------------------------------------------
    '----CALL
    '(assume oDR = datareader which is populated)

    m_sEmailFrom = ConvertNull(oDR("Email_From"))

    '----FUNCTION
    Public Function ConvertNull(ByVal piDatafield As Object)
    If IsDBNull(piDatafield) Then
    Return Nothing
    Else
    Return piDatafield
    End If
    End Function
    -------------------------------

    ASP.NET seems to use "nothing" as a value and an empty value (0 or "")
    interchangably.


    Thanks for any insight.

    Phil
    Phil Sandler, Aug 19, 2004
    #1
    1. Advertising

  2. Phil Sandler

    Phil Sandler Guest

    "HiltonG" <> wrote:
    > For strings specifically you can try return String.Empty


    My hope was to have a generic function to handle generic cases (at
    least common ones). So far it seems to work for integers, dates and
    strings.

    It returns nothing if the parameter is dbnull, and nothing seems to
    convert to the "empty" version of each datatype I have tried (date,
    int, string).

    Anyway, the questions remains if anyone sees a problem with doing it
    this way, or if there is a better way?

    (Since doing this, I have found convert.toint32 convert.tostring, etc.
    and am wondering if it does the same thing.)


    > > -------------------------------------------
    > > '----CALL
    > > '(assume oDR = datareader which is populated)
    > >
    > > m_sEmailFrom = ConvertNull(oDR("Email_From"))
    > >
    > > '----FUNCTION
    > > Public Function ConvertNull(ByVal piDatafield As Object)
    > > If IsDBNull(piDatafield) Then
    > > Return Nothing
    > > Else
    > > Return piDatafield
    > > End If
    > > End Function
    > > -------------------------------
    > >
    > > ASP.NET seems to use "nothing" as a value and an empty value (0 or "")
    > > interchangably.
    Phil Sandler, Aug 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    0
    Views:
    340
    Jason
    Oct 12, 2004
  2. Timo Nentwig
    Replies:
    31
    Views:
    1,188
    Chris Smith
    May 13, 2004
  3. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    0
    Views:
    368
    milkyway
    Dec 2, 2004
  4. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    2
    Views:
    332
  5. mike
    Replies:
    0
    Views:
    348
Loading...

Share This Page