Sorry, but this is a brain phart. I have searched the whole internet, but can't figure this out.
Error: "Unable to cast object of type 'System.DateTime' to type 'System.String'."
if (oDbDataReader.GetString(2) == DateTime.Now.AddDays(-90).ToShortDateString()) //DateCreated
{
oEmp.PasswordCompliance = "Password expired";
}
Two problems, first you are comparing for equality, if anyone checks after 3 months this will not trigger. Second, you probably are storing a Date
or DateTime
in you database, causing your call to GetString
to fail.
Use the following instead (no reason to do use strings in this case).
if (oDbDataReader.GetDateTime(2) <= DateTime.Now.AddDays(-90))
Also note that your original had an extra ;
potentially causing your password to always be expired.
What is the type of the column [2]? If it is DateTime
you should try DbDataReader.GetDateTime
if (oDbDataReader.GetDateTime(2) < DateTime.Now.AddDays(-90))
{
oEmp.PasswordCompliance = "Password expired";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With