VB.Net - checking for Null values in DataSets

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

    VB.Net - checking for Null values in DataSets

    Hi there.

    I wonder if there was a "correct answer" for Vb.Net (I understand this is
    quite easy in C#).

    If im drawing data from a database using a TableAdapter the data ends up in
    a DataTable. I then need to iterate through the DataTable to get the values
    out. This in itself is not a big issue.

    Code something like...

    For x = 0 to........

    myString = DataTable.Rows( x).Item("MyValu e")

    until I come across a Null value.

    A Null value in the Database throws an exception which is easy to address
    with a Try Catch statement. However as mentioned in some of the excellent
    articles this is not a good solution for the problem.

    Is there some means of testing the value before accessing it so that the
    Try/Catch would no longer be required.

    Perhaps something like:

    If Not Datatable.Rows( 0).Item("MyValu e") = Nothing Then

    myString = DataTable.Rows( 0).Item("MyValu e")

    End If

    Which doesnt work!

    Try/Catch just seems the wrong approach.


    Regards

    Martyn Fewtrell
  • Patrick.O.Ige

    #2
    Re: VB.Net - checking for Null values in DataSets

    Martyn trying checking fo DBNull.value
    Hope that helps
    Patrick

    "Martyn Fewtrell" <mfewtrell@news group.nospamwro te in message
    news:88DDAC44-6924-4C1A-AC78-DE07051E7526@mi crosoft.com...
    Hi there.
    >
    I wonder if there was a "correct answer" for Vb.Net (I understand this is
    quite easy in C#).
    >
    If im drawing data from a database using a TableAdapter the data ends up
    in
    a DataTable. I then need to iterate through the DataTable to get the
    values
    out. This in itself is not a big issue.
    >
    Code something like...
    >
    For x = 0 to........
    >
    myString = DataTable.Rows( x).Item("MyValu e")
    >
    until I come across a Null value.
    >
    A Null value in the Database throws an exception which is easy to address
    with a Try Catch statement. However as mentioned in some of the excellent
    articles this is not a good solution for the problem.
    >
    Is there some means of testing the value before accessing it so that the
    Try/Catch would no longer be required.
    >
    Perhaps something like:
    >
    If Not Datatable.Rows( 0).Item("MyValu e") = Nothing Then
    >
    myString = DataTable.Rows( 0).Item("MyValu e")
    >
    End If
    >
    Which doesnt work!
    >
    Try/Catch just seems the wrong approach.
    >
    >
    Regards
    >
    Martyn Fewtrell

    Comment

    • Cowboy \(Gregory A. Beamer\)

      #3
      Re: VB.Net - checking for Null values in DataSets

      Compare to DBNull.Value.

      --
      Gregory A. Beamer
      MVP; MCP: +I, SE, SD, DBA

      *************** *************** *************** ****
      Think outside of the box!
      *************** *************** *************** ****
      "Martyn Fewtrell" <mfewtrell@news group.nospamwro te in message
      news:88DDAC44-6924-4C1A-AC78-DE07051E7526@mi crosoft.com...
      Hi there.
      >
      I wonder if there was a "correct answer" for Vb.Net (I understand this is
      quite easy in C#).
      >
      If im drawing data from a database using a TableAdapter the data ends up
      in
      a DataTable. I then need to iterate through the DataTable to get the
      values
      out. This in itself is not a big issue.
      >
      Code something like...
      >
      For x = 0 to........
      >
      myString = DataTable.Rows( x).Item("MyValu e")
      >
      until I come across a Null value.
      >
      A Null value in the Database throws an exception which is easy to address
      with a Try Catch statement. However as mentioned in some of the excellent
      articles this is not a good solution for the problem.
      >
      Is there some means of testing the value before accessing it so that the
      Try/Catch would no longer be required.
      >
      Perhaps something like:
      >
      If Not Datatable.Rows( 0).Item("MyValu e") = Nothing Then
      >
      myString = DataTable.Rows( 0).Item("MyValu e")
      >
      End If
      >
      Which doesnt work!
      >
      Try/Catch just seems the wrong approach.
      >
      >
      Regards
      >
      Martyn Fewtrell

      Comment

      • Martyn Fewtrell

        #4
        Re: VB.Net - checking for Null values in DataSets

        Thanks to both of you.

        I've gone with

        If Not (IsDBNull(DataT able.Rows(x).It em("MyValue")) ) Then
        myString = DataTable.Rows( x).Item("MyValu e")
        End If

        Which seems to do the trick!

        --
        Regards

        Martyn Fewtrell


        "Cowboy (Gregory A. Beamer)" wrote:
        Compare to DBNull.Value.
        >
        --
        Gregory A. Beamer
        MVP; MCP: +I, SE, SD, DBA
        >
        *************** *************** *************** ****
        Think outside of the box!
        *************** *************** *************** ****
        "Martyn Fewtrell" <mfewtrell@news group.nospamwro te in message
        news:88DDAC44-6924-4C1A-AC78-DE07051E7526@mi crosoft.com...
        Hi there.

        I wonder if there was a "correct answer" for Vb.Net (I understand this is
        quite easy in C#).

        If im drawing data from a database using a TableAdapter the data ends up
        in
        a DataTable. I then need to iterate through the DataTable to get the
        values
        out. This in itself is not a big issue.

        Code something like...

        For x = 0 to........

        myString = DataTable.Rows( x).Item("MyValu e")

        until I come across a Null value.

        A Null value in the Database throws an exception which is easy to address
        with a Try Catch statement. However as mentioned in some of the excellent
        articles this is not a good solution for the problem.

        Is there some means of testing the value before accessing it so that the
        Try/Catch would no longer be required.

        Perhaps something like:

        If Not Datatable.Rows( 0).Item("MyValu e") = Nothing Then

        myString = DataTable.Rows( 0).Item("MyValu e")

        End If

        Which doesnt work!

        Try/Catch just seems the wrong approach.


        Regards

        Martyn Fewtrell
        >
        >
        >

        Comment

        Working...