【发布时间】:2016-05-25 15:44:55
【问题描述】:
我得到以下代码:
var model = new List<LogModels>();
while (reader.Read())
{
var logContent = new LogModels(
(int)reader["id"],
(string)reader["response"],
(string)reader["language"],
(string)reader["country"],
(string)reader["location"],
(string)reader["os"],
(string)reader["browser"],
(string)reader["type"],
(DateTime)reader["date"]
);
model.Add(logContent);
}
但我收到以下错误:
An exception of type 'System.InvalidCastException' occurred in Speed Services.dll but was not handled in user code
Additional information: Specified cast is not valid.
图片:https://i.gyazo.com/adf04b8e271bb53c93303d5774a48f80.png
我的模型(LogModels)如下所示:
public int id { get; set; }
public string response { get; set; }
public string language { get; set; }
public string country { get; set; }
public string location { get; set; }
public string os { get; set; }
public string browser { get; set; }
public string type { get; set; }
public DateTime date { get; set; }
由于我似乎找不到问题,有人知道问题可能是什么吗?
【问题讨论】:
-
完整的异常消息比 1000 张图像更有用,但我猜其中一个字段不是您期望的类型(消息会告诉您哪一个......)当然我在说关于阅读器对象(我想它是一个 DB DataReader),而不是您的 View Model 类 LogModels。
-
您应该缩小失败的范围。我敢打赌
id或date。 -
你检查过当前记录的
date是否为空吗?也许尝试将DateTime更改为可空类型(例如DateTime?) -
我如何准确地检查是哪一个导致了问题?我是 Visual Studio 的新手。这是数据库表:i.gyazo.com/3471b22ad642b7e12aab6fd2ac4111ab.png Nothing 似乎是 null?
标签: c# asp.net asp.net-mvc visual-studio