获取错误“无法将对象从DBNull强制转换为其他类型”

时间:2016-03-23 12:35:04

标签: c# linq datatable dbnull

我想获得每个 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

1 个答案:

答案 0 :(得分:1)

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

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