How to easily parse DBNull value into other data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hon Yuen, Ng

    How to easily parse DBNull value into other data type

    Hi

    This is a newbie question.
    I've a datatable that contains null value. Sometime i need to store this
    data into different variables. However, error will be thrown if the data
    i tried to stored in the variable is a null value.

    E.g.
    Dim int As Int64
    Dim null As Object
    null = DBNull.Value
    int = null 'this will throw error

    To solve this, i have to check if the value is DBNull each time. Is
    there a simpler way to do this?

    Thanks in advance.

    From,
    Hon Yuen, Ng
  • Michael C#

    #2
    Re: How to easily parse DBNull value into other data type

    I usually create a function like this:

    Function SafeString(ByVa l o As Object) As String
    Dim s As String = String.Empty
    If Not (o Is Nothing OrElse o Is DBNull.Value) Then
    s = Convert.ToStrin g(o)
    End If
    Return (s)
    End Function

    This function returns an empty string if you pass it an DBNull.Value. Also
    note that the object is first checked to make sure it is not Nothing. If
    you're pulling data from a SQL Server, you can use the COALESCE() function
    on the Server-Side to perform a similar function before it ever gets to your
    client app.

    Thanks,
    Mike C

    "Hon Yuen, Ng" <hyng@tm.net.my > wrote in message
    news:eAeMOnGIFH A.2648@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi
    >
    > This is a newbie question.
    > I've a datatable that contains null value. Sometime i need to store this
    > data into different variables. However, error will be thrown if the data i
    > tried to stored in the variable is a null value.
    >
    > E.g.
    > Dim int As Int64
    > Dim null As Object
    > null = DBNull.Value
    > int = null 'this will throw error
    >
    > To solve this, i have to check if the value is DBNull each time. Is there
    > a simpler way to do this?
    >
    > Thanks in advance.
    >
    > From,
    > Hon Yuen, Ng[/color]


    Comment

    • Michael C#

      #3
      Re: How to easily parse DBNull value into other data type

      P.S. - For Numeric values you can use a similar function, but return a 0 for
      DBNull.Value or Nothing.

      Thanks,
      Mike C.

      "Hon Yuen, Ng" <hyng@tm.net.my > wrote in message
      news:eAeMOnGIFH A.2648@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Hi
      >
      > This is a newbie question.
      > I've a datatable that contains null value. Sometime i need to store this
      > data into different variables. However, error will be thrown if the data i
      > tried to stored in the variable is a null value.
      >
      > E.g.
      > Dim int As Int64
      > Dim null As Object
      > null = DBNull.Value
      > int = null 'this will throw error
      >
      > To solve this, i have to check if the value is DBNull each time. Is there
      > a simpler way to do this?
      >
      > Thanks in advance.
      >
      > From,
      > Hon Yuen, Ng[/color]


      Comment

      • Hon Yuen, Ng

        #4
        Re: How to easily parse DBNull value into other data type

        Thanks for your reply.
        I think i understood what you mentioned. However, what if i do data
        binding? If i bind a data table to a TextBox, and the data is a
        DBNull.Value? What is the best practice to solve this kind of problem?

        Thanks in advance.

        From,
        Hon Yuen, Ng

        Comment

        • Cor Ligthert

          #5
          Re: How to easily parse DBNull value into other data type

          Hon,

          For that are the binding events

          In this message is a sample from me that I made for this.



          I hope this helps?

          Cor


          Comment

          Working...