【问题标题】:exception "nullable value must have a value" for datetime [duplicate]日期时间的异常“可为空值必须有值”[重复]
【发布时间】:2013-12-04 04:36:18
【问题描述】:

我正在尝试将“null”值分配给 Datetime? 对象。但它抛出"nullable value must have a value" 异常。

 //item.PlannedStartDate value is nothing.
 fact_Initiative.Start_Date = If([String].IsNullOrEmpty(item.PlannedStartDate), 
    DBNull.Value, CType(Convert.ToDateTime(item.PlannedStartDate), 
    System.Nullable(Of Date)))

如何解决这个问题

【问题讨论】:

  • 您确定 fact_Initiative.Start_Date 的类型为 Nullable?
  • 另外,我怀疑DBNull.Value 会在这里工作。你能展示一些上下文代码吗?我觉得如果我们还有更多工作要做的话,会有一个很好的解决方案在等待。
  • @AlexeyRaga 它是DateTime?

标签: c# asp.net vb.net


【解决方案1】:
fact_Initiative.Start_Date =If(String.IsNullOrEmpty(item.PlannedStartDate), CType(Nothing, DateTime?), DateTime.Parse(item.PlannedStartDate))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2011-08-22
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多