Search code examples
.netvb.netado.netdbnull

Problem Converting Byte() to Date()


I have a query in my DAL that results 1 item, a date. It's either a date or null. But I get an error when the value is null.

Conversion from type 'DBNull' to type 'Date' is not valid.

Query

Public Function GetOrderDepositByOrderID(ByVal OrderID As Integer) As Date
    myconn.Open()

    Dim date As Date

    Dim sql As String = "SELECT ifnull(OrderDeposit, '1900-01-01') FROM Order WHERE OrderID = ?"
    Dim cmd As New OdbcCommand(sql, myconn)

    cmd.Parameters.AddWithValue("OrderID", OrderID)

    date= cmd.ExecuteScalar()

    'connectie sluiten
    myconn.Close()

    Return date
End Function

This is how I call my function.

If bllCust.getOrderDepositByOrderID(OrderID) = DBNull Then
   lblBoodschap.Text = ("Deposit not paid.\n")
Else
   lblBoodschap.Text = ("Deposit paid.\n")
End If

If the deposit is paid there's a date in the table, if not it's left null.

All help is welcome!


Solution

  • Edited the database fields so that null values get a default value 1900-01-01, a date that will never be used.