2016-03-23 21 views
0

Her BusType alt toplamını almak istiyorum. Sorunum şu ki Object cannot be cast from DBNull to other types hatasını alıyorum. Breakpoint noktasını return ds'a koyduğumda DataTable üzerinde sonuç elde ediyorum. Devam etmeye çalıştığımda, bu hata Object cannot be cast from DBNull to orther types görüntüler. Bunu Nasıl Çözebilirim?Hata "Nesne DBNull'dan diğer türlere gönderilemiyor"

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; 

Gerekli Çıktı ilk önce null

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 
+0

Veri kümenizi veritabanına nereye atarsınız? Tek gördüğüm 'DataTable dt = yeni DataTable()' ve sonra bu bölümdeki boş tablo üzerinde yinelenmeye başlıyor –

+0

@AlexanderDerck bu kısımda 'ds.Tables.Add (query.CopyToDataTable()); ds.Tables [0] .TableName = "Tablo1"; 'veri kümemi atadığım yerde DataSetLinqOperator kullanıyorum. –

+0

Gördüğünüz gibi, "Tablo1" 'bir datatable veya bir şey için varsayılan addır? Benim kötü –

cevap

1

çek o dayanan kullanmaktır.

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