Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cast from string "" to type 'Date' is not valid

Author  Topic 

ncamoens
Starting Member

9 Posts

Posted - 2006-03-12 : 01:00:47
Hello, I am using an SQLDataReader called DrCoInfo to check if any matching records exist in a table.

My code is as such:

Line 119:DrCoInfo = comm1.ExecuteReader()
Line 120: While DrCoInfo.Read()
Line 121: If DrCoInfo("DateOfMan") <> "" Then
Line 122: BValid = True
Line 123: End If

However, it fails to work with the message :System.InvalidCastException: Cast from string "" to type 'Date' is not valid.

Any suggestions? Thanks.

Kristen
Test

22859 Posts

Posted - 2006-03-12 : 01:56:22
I'm not sure what a SQLDataReader is (Oracle perhaps??) but normally you have to use the syntax

MyColumn IS NULL

rather than comparing it again a blank string (which is not considered to be a valid date)

Kristen
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-03-12 : 02:23:42
quote:
Originally posted by Kristen

I'm not sure what a SQLDataReader is (Oracle perhaps??) but normally you have to use the syntax



SQLDataReader is the .net class which is used to communicate with the backend.

Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-12 : 09:05:26
Your are comparing a date to a string. If you want to do so, you need to convert to the date to a string first.

If DrCoInfo("DateOfMan").Tostring() <> "" Then

Of course, converting a date to a string will never return "". Are you trying to see if the date column is null? Then you need to check for that. Don't assume that Null is the same as "".

If IsDBNull(DrCoInfo("DateOfMan")) Then ...

Go to Top of Page

ncamoens
Starting Member

9 Posts

Posted - 2006-03-12 : 22:47:00
Thanks Jsmith...your first suggestion solved the problem.
Go to Top of Page
   

- Advertisement -