日期不接受空值

Date Does not accept null value

我正在创建一个网格视图,允许用户输入特定工件(有多个)的日期(例如完成日期)。如果我尝试输入 null 或空值,它会崩溃。我一直在寻找答案。他们输入的数据会更新到 sql 服务器数据库。我已经在 date 和 nvarchar 之间来回切换了数据。此外,该值必须绑定,以便它更新 ASP.net.

中的数据库

代码 ASP.NET

                <asp:TemplateField HeaderText="Verify Info Prod & Maturity Level" SortExpression="IPMaturity">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IPMaturity") %>' Width="75"></asp:TextBox>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Text="Complete" Checked='<%# Bind ("CheckBox1") %>'></asp:CheckBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblIPMat" runat="server" Text='<%# Eval("IPMaturity") %>' Width="75"></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle CssClass="verticaltext" Height="140px" Width="88px" HorizontalAlign="Center" VerticalAlign="Bottom" />
                </asp:TemplateField>

代码 C#

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity"));

  if (date1 >= DateTime.Now)
    e.Row.Cells[4].BackColor = System.Drawing.Color.White;

  if (date1 < DateTime.Now)
    e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
 }
}

我没有必要的项目来测试它,但我认为 If 语句测试它是 null 会起作用。如果这不起作用,我会使用 try/catch ,我也放在下面。如果您知道抛出的异常,最好能捕捉到它。

如果代码:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity"));

                if (date1 >= DateTime.Now)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.White;

                if (date1 < DateTime.Now)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
            }else
            {
                // Data was null, so do something?
            }
        }
    }

Try/Catch代码:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity"));

                if (date1 >= DateTime.Now)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.White;

                if (date1 < DateTime.Now)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
            }
        }
        catch
        {
            // Gracefully handle the exception here
        }
    }

您可能正在寻找可为空的日期时间:DateTime?

System.DateTime类型是值类型,所以它不允许为空。 Nullable class 允许将值设置为 null.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      string ipMaturity = DataBinder.Eval(e.Row.DataItem, "IPMaturity");
      DateTime? date1 = !string.IsNullOrEmpty(ipMaturity) ?  Convert.ToDateTime(ipMaturity) : (DateTime?)null;

  if (date1.HasValue && date1 >= DateTime.Now)
    e.Row.Cells[4].BackColor = System.Drawing.Color.White;

  if (date1.HasValue && date1 < DateTime.Now)
    e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
 }
}

现在我们正在使用可为空的 DateTime,更新 if 语句以检查 null 以避免在提供值之前设置背景颜色。

我对 DataBinder.Eval 不是很熟悉,如果不 return 和 string,我深表歉意,但希望 DateTime? 能为您指明正确的方向。

如果您需要将 null 保存为数据库中的值,数据库中的字段也需要为空。