【问题标题】:Getting error "Object cannot be cast from DBNull to other types"出现错误“对象无法从 DBNull 转换为其他类型”
【发布时间】:2016-03-24 17:09:03
【问题描述】:

我想获取每个 BusType 的小计。我现在的问题是我收到了错误Object cannot be cast from DBNull to other types。当我将断点放在return ds 上时,我确实在 DataTable 上得到了结果。当我尝试继续时,它会显示错误Object cannot be cast from DBNull to orther types。我该如何解决这个问题?

var query = (from _transaction in _entities.Transactions
    join _cd in _entities.Organisations on _transaction.Refno equals _cd.Refno
    join _b in _entities.BusinessType on _transaction.BusinessTypeCode equals _b.BusinessTypeCode
              group new
              {
                  _trans = _transaction,
                   cd = _cd,
               }
               by new { _transaction.BusinessTypeCode,_transaction.Refno, _cd.BusinessName, _b.Description } into _group
               orderby _group.Key.BusinessTypeCode
               select new
                       {
                          BusinessTypeCode = _group.Key.BusinessTypeCode,
                          BusType = _group.Key.BusinessTypeCode + " - " +_group.Key.Description,
                          BusName = _group.Key.BusinessName,
                          BusL = _group.Sum(x=>x._trans.BusL),
                          BusInterrest = _group.Sum(x => x._trans.BusInterrest),
                          BusAdmin = _group.Sum(x => x._trans.BusAdmin),
                          BusPenalty = _group.Sum(x => x._trans.BusPenalty),
                          TotalBusCollected =_group.Sum(x=>x._trans.TotalBusCollected)
                       });
                DataTable dt=new DataTable();
                DataSet ds = new DataSet();
                ds.Tables.Add(query.CopyToDataTable());
                ds.Tables[0].TableName = "Table1";
                dt=ds.Tables[0];
    double BusL=0; double BusInterrest=0; double BusAdmin=0;
    double BusPenalty=0; double TotalBusCollected=0; string Title = string.Empty;
for(int i=0; i<=dt.Rows.Count - 1; i++)
{
  if(i > 0)
  {
     if(dt.Rows[i]["BusType"].ToString().ToLower() != dt.Rows[i]]["BusType"].ToString().ToLower())
     {
        dt.Rows.InsertAt(dt.NewRow(),i);
        dt.Rows[i]["BusType"]=Title,
        Title = string.Empty;        
        dt.Rows[i]["BusL"]=BusL;
        BusL=0;
        dt.Rows[i]["BusInterrest"]=BusInterrest;
        BusInterrest=0;
        dt.Rows[i]["BusAdmin"]=BusAdmin;
        BusAdmin=0;
        dt.Rows[i]["BusPenalty"]=BusPenalty;
        BusPenalty=0;
        dt.Rows[i]["TotalBusCollected"]=TotaBusCollected;
        TotalBusCollected=0;
        i++;
     }
   }
   Title = "Subtotal for "+dt.Rows[i]["BusType"];
   BusL +=Convert.ToDouble(dt.Rows[i]["BusL"]);
   BusInterrest+=Convert.ToDouble(dt.Rows[i]["BusInterrest"]);
   BusAdmin+=Convert.ToDouble(dt.Rows[i]["BusAdmin"]);
   BusPenalty+=Convert.ToDouble(dt.Rows[i]["BusPenalty"]);
   TotalBusCollected+=Convert.ToDouble(dt.Rows[i]["TotalBusCollected"])
}
        return ds;

需要的输出

   BusType     |BusName   | BusL  |BusInterest|BusAdmin| BusPenalty|TotalBusCollected
1 - ACCOUNTING |HIGHVELD  |-23.91 | 0         |-22.84  | 0         |-46.75
1 - ACCOUNTING |BHP       |-50.81 |-79.21     |-76     |-20.02     |-226.04
--------------------------+-------+-----------+--------+-----------+-----------------
Sub Total                 |-74.72 |-79.21     |-98.84  |-20.02     |-272.79
--------------------------+-------+-----------+--------+-----------+-----------------
 2 - FOOD      |SAB       |-14.18 |-435.97    |-2.57   |-67.55     |-520.27
 2 - FOOD      |DISTIL    |-43.05 |0          |-66,59  |0          |-109.64
--------------------------+-------+-----------+--------+-----------+-----------------
Sub Total                 |-57.23 |-435.97    |-69.16  |-67.55     |-629.91
--------------------------+-------+-----------+--------+-----------+-----------------
3 - MINING     |ANGLOGOLD |-4.43  |0          |-72     |0          |-76.43
--------------------------+-------+-----------+--------+-----------+-----------------
Sub Total                 |-4.43  |0          |-72     |0          |-76.43

【问题讨论】:

  • 您在哪里将数据集分配给数据表?我看到的只是DataTable dt = new DataTable() 然后你开始遍历空表
  • @AlexanderDerck 在这部分 ds.Tables.Add(query.CopyToDataTable());ds.Tables[0].TableName = "Table1"; 我分配我正在使用 DataSetLinqOperator 的数据集的位置。
  • 哦,我明白了,"Table1" 是数据表的默认名称还是什么?我的坏
  • @AlexanderDerck Table1 我在这里给出了数据表的名称。我正在为我的桌子命名。
  • 很抱歉让我很头疼,但我在 msdn 上看到了这个:To add tables to the collection, use Add method of the DataTableCollection. To remove tables, use the Remove method. 你确实添加了一个表并给它命名,但你 nowhere 说该表是dt,那么您如何迭代它呢?请赐教:p

标签: c# linq datatable dbnull


【解决方案1】:

首先检查 null 然后根据它使用它。

if (Convert.IsDBNull(dt.Rows[i]["BusType"])){  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2011-08-31
    相关资源
    最近更新 更多