我有一个名为Project的对象。
在Project中,我有一个像这样的属性定义:
while (dr.Read())
{
#region Fill Project object
try
{
projects.Add(new Project
{
FyTotalCost = (dr["fy_total_cost"] != null
&& dr["fy_total_cost"].ToString() != string.Empty
&& dr["fy_total_cost"].ToString() != "") ?
decimal.Parse(dr["fy_total_cost"].ToString()) :
(decimal?)null,
然后我翻阅一个datareader来填充一个对象列表。
dr["fy_total_cost"] != null
&& dr["fy_total_cost"].ToString() != string.Empty
&& dr["fy_total_cost"].ToString() != ""
在快速监视中评估为false:
decimal.Parse(dr["fy_total_cost"].ToString())
但它似乎正在执行:
(decimal?)null
而不是:
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
fy:
fy_total_cost:
budget_start_date:
budget_end_date:
Error Message: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options,
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at TransferProjects.Program.FillProjectsModel() in c:\Development\TransferProjects\TransferProjects\Program.cs:line 172
我跟踪输出到输出窗口:
这就是我得到的:
{{1}}
答案 0 :(得分:2)
因为数据库中的null实际上是DBNull.Value。你应该检查一下:
FyTotalCost = (dr["fy_total_cost"] != DBNull.Value
...