[英]Unable to cast object of type 'System.DateTime' to type 'System.String'” error
[英]asp.net error: Unable to cast object of type 'System.DateTime' to type 'System.String'
对不起,但这是一个大脑镜头。 我搜索了整个互联网,但无法弄清楚这一点。
错误: “无法将'System.DateTime'类型的对象强制转换为'System.String'。”
if (oDbDataReader.GetString(2) == DateTime.Now.AddDays(-90).ToShortDateString()) //DateCreated
{
oEmp.PasswordCompliance = "Password expired";
}
两个问题,首先你要比较平等,如果有人在3个月后检查,这将不会触发。 其次,您可能在数据库中存储Date
或DateTime
,导致您对GetString
的调用失败。
请改用以下内容(在这种情况下没有理由使用字符串)。
if (oDbDataReader.GetDateTime(2) <= DateTime.Now.AddDays(-90))
另请注意,您的原件有额外的;
可能导致您的密码始终过期。
列的类型是什么[2]? 如果是DateTime
您应该尝试DbDataReader.GetDateTime
if (oDbDataReader.GetDateTime(2) < DateTime.Now.AddDays(-90))
{
oEmp.PasswordCompliance = "Password expired";
}
阅读器中位置2的列中的值是DateTime,而不是字符串。 所以GetString调用失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.