【问题标题】:Cannot cast DBNull.Value to type 'System.DateTime', Please use nullable types无法将 DBNull.Value 转换为类型“System.DateTime”,请使用可为空的类型
【发布时间】:2015-09-29 06:22:13
【问题描述】:

日期为 null 时出现错误。错误行- DateTimerenewalDate = row.Field("RenewalDate");

protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        DateTime renewalDate = row.Field<DateTime>("RenewalDate");
        if (renewalDate.Date > DateTime.Today)
            e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#669B1F");
        else
            e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF8234");
        }
    }
}

【问题讨论】:

  • 阅读错误信息。错误消息告诉你该怎么做。
  • 使用可为空的日期时间DateTime? renewalDate = row.Field&lt;DateTime?&gt;("RenewalDate");

标签: c# asp.net datetime casting


【解决方案1】:

你不能将 null 转换为日期,让日期时间可以为空

 DateTime? renewalDate = row.Field<DateTime?>("RenewalDate")

也在你的 if 语句中

 if (renewalDate.HasValue && renewalDate.Value.Date > DateTime.Today)

【讨论】:

  • 我不能使用 var 。我正在使用 3.5 框架@user5114524
  • 日期时间?续订日期 = row.Field("续订日期"); if (renewalDate > DateTime.Today) e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#669B1F");否则正确吗?
  • 你需要在你的 if stmt 中考虑空值
  • 更改如果:if (renewalDate.HasValue &amp;&amp; renewalDate.Value.Date &gt; DateTime.Today)
【解决方案2】:

我认为这个错误很明显。您不能将 NULL 值分配给 DateTime。你有两个选择:

  1. 在数据库中创建字段NOT NULL,以确保它不能返回NULL
  2. 使用DateTime? 代替DateTime

    DateTime? renewalDate = row.Field&lt;DateTime?&gt;("RenewalDate");

【讨论】:

    【解决方案3】:

    使用它作为你的日期时间减速

     DateTime? renewalDate = row.Field<DateTime?>("RenewalDate");
    

    【讨论】:

    • 错误 25 'System.Nullable' 不包含“Date”的定义,并且没有扩展方法“Date”接受“System.Nullable' 可以找到(您是否缺少 using 指令或程序集引用@sanu
    • @krishnamohan - 你可以使用renewalDate.Value.Date,但首先检查renewalDate.HasValue,看看你是否真的一个值。
    【解决方案4】:

    我已经写了,如果有任何错误请建议我。用于空异常检查

     protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    
    
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        DataRow row = ((DataRowView)e.Row.DataItem).Row;
                      //  DateTime renewalDate = row.Field<DateTime>("RenewalDate");
                        DateTime? renewalDate = row.Field<DateTime?>("RenewalDate");
                        if (renewalDate > DateTime.Today)
                            e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#669B1F");
                        else
                            e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF8234");
    
                    }
    
    
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 2021-03-26
      • 2017-07-30
      • 1970-01-01
      相关资源
      最近更新 更多