我该如何解决这个错误。无法将对象从dbnull转换为其他类型 [英] How do I solve this error. Object cannot be cast from dbnull to orther types
问题描述
我有一个问题,我想得到每个** BusType **的小计。我现在的问题是我收到此错误`对象无法从DBNull转换为其他类型`。当我试图继续显示错误`对象不能从DBNull转换为其他类型`时,我将断点放在`return ds`上时,我确实得到了DataTable的结果。我该如何解决这个问题。
需要的输出
BusType | BusName | BusL | BusInterest | BusAdmin | BusPenalty | TotalBusCollected
1 - 会计| HIGHVELD | -23。 91 | 0 | -22。 84 | 0 | -46。 75
1 - 会计|必和必拓| -50。 81 | -79。 21 | -76 | - 20. 02 | -226。 04
- ------------------------ + ------- + - ---------- + -------- + ----------- + -----------------
小计| -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
- ---------------- -------- ------- + + ----------- + -------- + ----------- + -----------------
小计| -57。 23 | -435。< span class =code-digit> 97 | -69。 16 | -67。 55 | -629。 91
- ------------------------ + ------- + ----------- + ---- ---- + ----------- + -----------------
3 - MINING | ANGLOGOLD | -4。 43 | 0 | -72 | 0 | -76。 43
- ------------------------ + ----- - + ----------- + -------- + ----------- -------------- + ---
小计| -4。 43 | 0 | -72 | 0 | -76。 43
Code
var query =( from _transaction in _entities.Transactions
join _cd _entities.Organisations on _transaction.Refno equals _cd.Refno
join _b _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
选择 new
{
BusinessTypeCode = _group.Key.BusinessTypeCode,
BusType = _group.Key.BusinessTypeCode + - + _group.Key.Description,
BusName = _group.Key.BusinessName,
BusL = _group.Sum(X =&安培; GT; x._trans.Bu sL),
BusInterrest = _group.Sum(x =& gt; x._trans.BusInterrest),
BusAdmin = _group.Sum(x =& gt; x._trans.BusAdmin),
BusPenalty = _group.Sum(x =& gt; x._trans。 BusPenalty),
TotalBusCollected = _group.Sum(x =& gt; x._trans.TotalBusCollected)
});
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(query.CopyToDataTable());
ds.Tables [ 0 ]。TableName = 表1跨度>;
double BusL = 0 ; double BusInterrest = 0 ; double BusAdmin = 0 ;
double BusPenalty = 0 ; double TotalBusCollected = 0 ; string Title = string .Empty;
for ( int I = 0 ; I& lt; = dt.Rows.Count - 1 ; i ++)
{
if (i& gt; 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 = 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 = 小计 + 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])
}
返回 ds;
< b>我的尝试:
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 = 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 =小计为+ 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。行[i] [TotalBusCollected])
}
返回ds;
DataRow-cell(通常,在这里,显然)包含DBNull
而不是null
。 (确切地说,它包含类DBNull的单个实例。)
如果你没想到这种可能性,你可能会遇到数据完整性问题而你可能想要检查是否要使某些列不可为空并插入0(零)而不是空(null)。
如果要允许空值,你必须照顾这种可能性。最好的方法,也取代你的Convert.ToDouble(..) - 调用是使用通用字段< T>(列)
-methodDataRow
:
double busL = dt.Rows [i ] .Field< double>( BusL);
如果行单元格包含DBNull,则返回double
的默认值(即0),否则返回包含的double值。
@Sacha Lefevre我仍在犯同样的错误
Title = 小计 + dt.Rows [i] .Field< string>( BusType);
BusL + = dt.Rows [i] .Field< double>( BusL );
BusInterrest + = dt.Rows [i] .Field< double>( BusInterrest) ;
BusAdmin + = dt.Rows [i] .Field< double>( BusAdmin) ;
BusPenalty + = dt.Rows [i] .Field< double>( BusPenalty) ;
TotalBusCollected + = dt.Rows [i] .Field< double>( TotalBusCollected) ;
I have a problem here i want to get the subtotal of each **BusType**. My problem now is that i am getting this error `Object cannot be cast from DBNull to orther types`. I do get the result on DataTable when i put the Breakpoint on `return ds` when i am trying to proceed it display that error `Object cannot be cast from DBNull to orther types` . How can i solve this.
Needed Output
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
--------------------------+-------+-----------+--------+-----------+-----------------
Subtotal |-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
--------------------------+-------+-----------+--------+-----------+-----------------
Subtotal |-57.23 |-435.97 |-69.16 |-67.55 |-629.91
--------------------------+-------+-----------+--------+-----------+-----------------
3 - MINING |ANGLOGOLD |-4.43 |0 |-72 |0 |-76.43
--------------------------+-------+-----------+--------+-----------+-----------------
Subtotal |-4.43 |0 |-72 |0 |-76.43
Code
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";
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;
What I have tried:
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;
A DataRow-cell (usually, and here, apparently) containsDBNull
instead ofnull
. (To be exact, it contains the single instance of the class DBNull.)
If you didn't expect this possibility, you might have a data integrity problem and you might want to check if you want to make some columns non-nullable and insert 0's (zeroes) instead of nothing (null).
If you want to allow nulls, you have to take care for that possibility. The best way for that, which also replaces your Convert.ToDouble(..)-calls is to use the genericField<T>(column)
-method ofDataRow
:
double busL = dt.Rows[i].Field<double>("BusL");
This will return the default value fordouble
(which is 0) in case the row-cell contains DBNull and the contained double-value otherwise.
@Sacha Lefevre I am still geting the same error
Title = "Subtotal for "+dt.Rows[i].Field<string>("BusType"); BusL +=dt.Rows[i].Field<double>("BusL"); BusInterrest+=dt.Rows[i].Field<double>("BusInterrest"); BusAdmin+=dt.Rows[i].Field<double>("BusAdmin"); BusPenalty+=dt.Rows[i].Field<double>("BusPenalty"); TotalBusCollected+=dt.Rows[i].Field<double>("TotalBusCollected");
这篇关于我该如何解决这个错误。无法将对象从dbnull转换为其他类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!