用DBNULL检查ASP.Net值

时间:2010-03-23 16:11:31

标签: asp.net dbnull

我有以下代码

foreach (DataRowView dr in Data)
        {
            if (dr == System.DBNull.Value)
            {
                nedID = 1;
            }
        }

但是我收到以下错误 运算符==无法应用于System.Data.DataRowViewSystem.DBNull类型的操作数

请有人建议我如何检查值是否为空或DBNULL

2 个答案:

答案 0 :(得分:13)

您需要指定字段名称或索引。

foreach (DataRowView dr in Data)
{
    if (dr["nameOfField"] == System.DBNull.Value)
    {
        nedID = 1;
    }
}

答案 1 :(得分:5)

您需要将dr == System.DBNull.Value替换为...

Convert.IsDBNull(dr["somefield"])

如果是DBNnull

则返回true