Error message - Can someone check my code?

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.
 
M

Mr. Arnold

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.
 
C

Cor Ligthert[MVP]

Sheldon,

Use is DBnull.value instead of is nothing

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

Cor
 
H

Herfried K. Wagner [MVP]

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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top