locked
Replacing Empty String with DBNull.value RRS feed

  • Question

  • User2023521679 posted

    I'm having a problem trying to come up with a way to come up with a function to either return a DateTime value or DBNull.Value.

    In the following code exerpt, DData("HireDate") is a DateTime field in a column of a DataRow. HireDate can either be a Date Value or an empty string. If I try DData("HireDate") = IData("HireDate"), this errors out when it is string.empty. I though the following code would work, but I get the error message Operator '=' is not defined for type 'DBNull' and string "".

    DData("HireDate") = IIf(IData("HireDate") = "", DBNull.Value, IData("HireDate"))

    Thursday, June 27, 2013 1:31 PM

Answers

  • User281315223 posted

    You may want to try using the String.IsNullOrEmpty() method

    DData("HireDate") = IIf(String.IsNullOrEmpty(IData("HireDate")), DBNull.Value, IData("HireDate"))

    if that doesn't work, you may need to use the ToString() method on your objects as well : 

    DData("HireDate") = IIf(String.IsNullOrEmpty(IData("HireDate").ToString()), DBNull.Value, IData("HireDate").ToString())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 2:05 PM

All replies

  • User281315223 posted

    You may want to try using the String.IsNullOrEmpty() method

    DData("HireDate") = IIf(String.IsNullOrEmpty(IData("HireDate")), DBNull.Value, IData("HireDate"))

    if that doesn't work, you may need to use the ToString() method on your objects as well : 

    DData("HireDate") = IIf(String.IsNullOrEmpty(IData("HireDate").ToString()), DBNull.Value, IData("HireDate").ToString())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 2:05 PM
  • User2023521679 posted

    Yes, I actually tried something similar to that between my original post and your answer and it worked. Thanks for the reply.

    Thursday, June 27, 2013 2:07 PM