简体   繁体   English

C#:对象不能从 DbNull 转换为其他类型

[英]C#: Object cannot be cast from DbNull to other types

I have read several posts on this online, but none of them have resolved my issue.我在网上阅读了几篇关于此的帖子,但没有一篇文章解决了我的问题。 Most of the questions posted online point me towards checking Dbnull values from the database and I am doing that in the code.在线发布的大多数问题都指向我检查数据库中的 Dbnull 值,我正在代码中这样做。

Here's the code where the exception is thrown:这是抛出异常的代码:

int rowNum = Convert.ToInt32(dataTable.Rows[r][dataTable.Columns.Count - 2]);

Here's the code where I am checking for the dbnull values:这是我检查 dbnull 值的代码:

for (int r = 0; r < dataTable.Rows.Count - 1; r++) //rows
{
    for (int c = 1; c < dataTable.Columns.Count - 2; c++)
    {
        object val = dataTable.Rows[r][c];
        if (!val.Equals(DBNull.Value))
        {
            haveValues = true;
        }
    }
}

Here I am reading the values from the excel spreadsheet.在这里,我正在读取 Excel 电子表格中的值。

Please point me in the right direction.请指出我正确的方向。 Thanks in advance.提前致谢。

Dimpy昏暗的

check for DbNull before calling Convert.ToInt32: as you have seen, this will raise an exception if the value is DbNull.调用 Convert.ToInt32之前检查 DbNull:如您所见,如果值为 DbNull,这将引发异常。 something like:就像是:

object x = *value from db*
int y;
if (x != DbNull.Value)
    y= Convert.ToInt32(x);
else
    //handle null somehow

You can also check:您还可以检查:

dataTable.Rows[r].IsNull(c)

c can be the index, the column name or the DataColumn c可以是索引、列名或 DataColumn

for (int c = 1; c < dataTable.Columns.Count - 2 ; c++) for (int c = 1; c < dataTable.Columns.Count - 2 ; c++)

You don't check for count-2 so dataTable.Rows[r][ dataTable.Columns.Count - 2 ] could be DBNull and your Convert fails您不检查 count-2,因此 dataTable.Rows[r][ dataTable.Columns.Count - 2 ] 可能是 DBNull 并且您的 Convert 失败

I tend to do if(varFromSQL.ToString()!="") and then carry on with my business.我倾向于做 if(varFromSQL.ToString()!="") 然后继续我的业务。 Works like a charm every time.每次都像魅力一样工作。 And then if i need to System.Convert.ToSomething() i can do that.然后如果我需要 System.Convert.ToSomething() 我可以做到。

You can try like this: DBNull.Value.Equals(dataTable.Rows[r][c]) if you want the index of the column you can use row[dataTable.Columns.IndexOf(col)] inside the foreach.你可以这样尝试: DBNull.Value.Equals(dataTable.Rows[r][c]) 如果你想要列的索引,你可以在 foreach 中使用 row[dataTable.Columns.IndexOf(col)] 。

for (int r = 0; r < dataTable.Rows.Count - 1; r++) //rows
        {

            foreach (DataColumn col in dataTable.Rows[r].Table.Columns)
            {
                if (!DBNull.Value.Equals(dataTable.Rows[r][col.ColumnName]))
                {
                    haveValues = true;

                }
            }

And Please provide more information about the type of exception, maybe the error is accessing an invalid index => int rowNum = Convert.ToInt32(dataTable.Rows[r][dataTable.Columns.Count - 2]);并且请提供有关异常类型的更多信息,也许错误是访问无效索引 => int rowNum = Convert.ToInt32(dataTable.Rows[r][dataTable.Columns.Count - 2]); Why you use ...Columns.Count - 2 ?, What about if the count of columns is equals to 1 ?为什么要使用 ...Columns.Count - 2 ?,如果列数等于 1 呢?

I Hope to this helps.我希望这会有所帮助。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM