对象数据库枚举错误



我试图在互联网上遵循一些例子,但没有成功

我正在GridView1_RowDataBound方法中尝试以下内容

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        if (Convert.ToDateTime(drv["Created_Date"]).ToString("dd-MM-yyyy") == "01-01-1900" || (drv["Created_Date"].ToString().Equals(DBNull.Value))) //Check for this date
        {
            e.Row.Cells[11].Text = "test";
        }
    }
}

我仍然收到一个对象无法从 DBNull 转换为其他类型的对象。

你必须使用

string.IsNullOrEmpty(drv["Created_Date"].ToString())

但是,您应该切换 if 语句中的第一个和第二个值。您正在检查Created_Date在投射后是否null。所以如果它是null,你的代码仍然会抛出错误。所以最好做这样的事情

if (string.IsNullOrEmpty(drv["Created_Date"].ToString()) || Convert.ToDateTime(drv["Created_Date"]) == new DateTime(1900, 1, 1))

最新更新