asp.net error: Unable to cast object of type 'System.DateTime' to type 'System.String'

asp.net, c#, datetime

Solution

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.

Problem

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"; } ```

Original source