Asp.net error: Unable to overlay object of type "System.DateTime" on type "System.String"

Sorry, but this is the brain. I searched the entire internet but cannot figure it out.
Error: "Unable to pass an object of type" System.DateTime "to enter" System.String ".

if (oDbDataReader.GetString(2) == DateTime.Now.AddDays(-90).ToShortDateString()) //DateCreated { oEmp.PasswordCompliance = "Password expired"; } 
+4
source share
3 answers

Two problems, first you compare for equality, if someone checks after 3 months, this will not work. Secondly, you are probably storing a Date or DateTime in your database, as a result of which your GetString call will fail.

Use the following instead (there is no reason to use strings in this case).

 if (oDbDataReader.GetDateTime(2) <= DateTime.Now.AddDays(-90)) 

Also note that your original had an extra ; which potentially caused your password to expire.

+5
source

What is the type of column [2]? If it's a DateTime , you should try DbDataReader.GetDateTime

 if (oDbDataReader.GetDateTime(2) < DateTime.Now.AddDays(-90)) { oEmp.PasswordCompliance = "Password expired"; } 
+4
source

The value in the column at position 2 in the reader is a DateTime, not a row. Therefore, the call to GetString fails.

0
source

All Articles