Error message - Can someone check my code?

  • Thread starter Thread starter Sheldon
  • Start date Start date
S

Sheldon

Hello -

The following code is returning the error "Invalid cast exception.
Conversion from type DBNull to type Date is not valid." It will return the
recordset if DateWritten has a date in it, but if it does not, I get the
error. Any help will be appreciated1

<snip>
If dsEmailSearch.Tables("tblEmailAdd").Rows(currentEmailRow)("DateWritten")
Is Nothing = True Then
dtpWritten.Value =
dsEmailSearch.Tables("tblEmailAdd").Rows(currentEmailRow)("DateWritten")
txtWritten.Hide()
dtpWritten.Show()
Else
txtWritten.Show()
dtpWritten.Hide()
End If
<snip>

BTW, the data is from tables in a Sql Server 2005 database.
 
Sheldon said:
Hello -

The following code is returning the error "Invalid cast exception.
Conversion from type DBNull to type Date is not valid." It will return the
recordset if DateWritten has a date in it, but if it does not, I get the
error. Any help will be appreciated1

<snip>
If dsEmailSearch.Tables("tblEmailAdd").Rows(currentEmailRow)("DateWritten")
Is Nothing = True Then
dtpWritten.Value =
dsEmailSearch.Tables("tblEmailAdd").Rows(currentEmailRow)("DateWritten")
txtWritten.Hide()
dtpWritten.Show()
Else
txtWritten.Show()
dtpWritten.Hide()
End If
<snip>

BTW, the data is from tables in a Sql Server 2005 database.

The "DateWritten" is a NULL value. You can't cast a field that is NULL
to an object which is dtpWritten.

You're going to have to make a check for DBnull on the field. And if
it's DBNull, you're going to have to go around the statement for the
field and not try to cast it or address the field.
 
Sheldon,

Use is DBnull.value instead of is nothing

And don't forget to set Option Strict On in top of your progeram.

Cor
 
Sheldon said:
The following code is returning the error "Invalid cast exception.
Conversion from type DBNull to type Date is not valid." It will return
the
recordset if DateWritten has a date in it, but if it does not, I get the
error. Any help will be appreciated1

<snip>
If
dsEmailSearch.Tables("tblEmailAdd").Rows(currentEmailRow)("DateWritten")
Is Nothing = True Then

=> 'If ... Is DBNull.Value Then' or 'If IsDBNull(...) Then'.
 
Back
Top