locked
Testing for DBNull Throws Exception RRS feed

  • Question

  • User482458310 posted

    I'm using VBnet with Access Database

    I load a datatable with records that have DBNull in some fields (setting default value = "" doesn't stop this from happening on new record).

    So I tediously check fields as follows:

    Try

                Dim lokta As New LicDataSetTableAdapters.LockerTableAdapter
                Dim lokdt As New LicDataSet.LockerDataTable
                Dim lokdr As LicDataSet.LockerRow
                lokdt = lokta.GetOnePz(0) 'gets a record
                lokdr = lokdt.Rows(0)
                With lokdr
                If IsDBNull(.pzkey) Then
                    .pzkey = ""
                End If
    Catch ex As Exception

    End Try

    What happens is that I get an exception telling me that the field I'm testing for DBNull is DBNull.

    How can I test a field for DBNull and change it to "" without throwing an exception?

    Advice appreciated.


     

     

    Saturday, June 30, 2012 8:33 PM

Answers

  • User1759999623 posted

    What is the defined data type for the pzkey property?

    What is the data type of the underlying column from the database table?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2012 4:41 PM

All replies

  • User1759999623 posted

    Verify that lokdr is not null/Nothing.

    Saturday, June 30, 2012 9:21 PM
  • User482458310 posted

    I did check that --

    lokr has other fields that are showing expected values

    I actually manually changed the fields from DBNull to "" in the database, and the error condition did not show up.

    Thanks for the suggestion.

     

    Saturday, June 30, 2012 9:51 PM
  • User1759999623 posted

    In place of the IsDBNull VB function, try using either the Convert.IsDBNull or DBNull.Value.Equals method.

    Saturday, June 30, 2012 10:55 PM
  • User482458310 posted

    I tried both Convert.IsDBNull and DBNull.Value.Equals methods with same result --

    StrongTypingException was caught.
    The value for column 'pzkey' in table 'Locker' is DBNull.
    InnerException: Make sure the source type is convertible to the destination type.

    I closed and reloaded the IDE in case of corruption but still get this exception.

     

     

    Sunday, July 1, 2012 12:15 PM
  • User1759999623 posted

    What is the defined data type for the pzkey property?

    What is the data type of the underlying column from the database table?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2012 4:41 PM
  • User482458310 posted

    In Access the table 'Locker' has field pzkey defined as Text, Field Size 50, Default Value "", Required-No, Allow Zero Length-Yes, Indexed(Duplicates OK)

    Aha -- I see that I can change the NullValue property of columns in the datatable from (Throw exception) to (Empty).  This seems to convert a null valued field to "", and seems to survive a reconfigure of the table adapter, so with this setting I don't need to check for Null at all. 

    Thanks for prodding me to look in the right direction.

     

     

    Sunday, July 1, 2012 5:40 PM