Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将DBNull.Value强制转换为类型';System.DateTime';,请使用可为空的类型_C#_Asp.net_Datetime_Casting - Fatal编程技术网

C# 无法将DBNull.Value强制转换为类型';System.DateTime';,请使用可为空的类型

C# 无法将DBNull.Value强制转换为类型';System.DateTime';,请使用可为空的类型,c#,asp.net,datetime,casting,C#,Asp.net,Datetime,Casting,日期为空时获取错误。错误行-DateTime renewalDate=行字段(“renewalDate”) 受保护的void GrdV\u Projects\u RowDataBound(对象发送方,GridViewRowEventArgs e) { 如果(e.Row.RowType==DataControlRowType.DataRow) { 如果(e.Row.RowType==DataControlRowType.DataRow) { DataRow row=((DataRowView)e.

日期为空时获取错误。错误行-DateTime renewalDate=行字段(“renewalDate”)

受保护的void GrdV\u Projects\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
DataRow row=((DataRowView)e.row.DataItem).row;
DateTime renewalDate=行字段(“renewalDate”);
如果(renewalDate.Date>DateTime.Today)
e、 Row.Cells[7]。BackColor=System.Drawing.ColorTranslator.FromHtml(“#669B1F”);
其他的
e、 Row.Cells[7]。BackColor=System.Drawing.colorplator.FromHtml(“#FF8234”);
}
}
}

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

  • 使数据库中的字段
    不为NULL
    ,以确保它不能返回
    NULL
  • 使用
    DateTime?
    代替
    DateTime


    DateTime?renewalDate=行字段(“renewalDate”)


  • 不能将null转换为日期,使日期时间为空

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

    将此用作日期时间减速

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

    DateTime?renewalDate=行字段(“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");
                        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");
    
                    }
    
    
    
            }
        }
    
    受保护的void GrdV\u Projects\u RowDataBound(对象发送方,GridViewRowEventArgs e)
    {
    如果(e.Row.RowType==DataControlRowType.DataRow)
    {
    如果(e.Row.RowType==DataControlRowType.DataRow)
    {
    DataRow row=((DataRowView)e.row.DataItem).row;
    //DateTime renewalDate=行字段(“renewalDate”);
    
    DateTime?renewalDate=行字段(“renewalDate”); 如果(renewalDate>DateTime.Today) e、 Row.Cells[7]。BackColor=System.Drawing.ColorTranslator.FromHtml(“#669B1F”); 其他的 e、 Row.Cells[7]。BackColor=System.Drawing.colorplator.FromHtml(“#FF8234”); } } }
    读取错误消息。错误消息告诉您要做什么。是否使用可为空的日期时间
    DateTime?renewalDate=行字段(“renewalDate”)错误25'System.Nullable'不包含'Date'的定义,并且找不到接受'System.Nullable'类型的第一个参数的扩展方法'Date'(是否缺少using指令或程序集referenence@sanu@krishnamohan-您可以使用
    renewalDate.Value.Date
    ,但首先检查
    renewalDate.HasValue
    以查看您是否真的有值。我不能使用var.im使用3.5framework@user5114524DateTime?更新日期=行字段(“更新日期”);如果(EngReDease:DeaTime.今天)Eng.Real[7 ] .BuffCale= Stask.Diga.Curror Primor . FromHtml(“yx69b1f”),否则是正确的吗?您需要考虑IF STMTREST中的NULL值,如果:<代码> if(RealWaldas.HasValue&RealWaldAdv.Deal> DeaTime.今天)
     DateTime? renewalDate = row.Field<DateTime?>("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");
                        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");
    
                    }
    
    
    
            }
        }