繁体   English   中英

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

[英]Getting error “Object cannot be cast from DBNull to other types”

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

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

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

暂无
暂无

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

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