[英]StrongTypingException when setting column value
Can anyone please tell me why I get a StrongTypingException when ASSIGNING a value to a column in a strongly typed DataTable? 任何人都可以告诉我为什么在将值赋值给强类型DataTable中的列时会出现StrongTypingException? (I understand why I get it if I were to READ the column with DBNull value)
(我明白为什么如果我用DBNull值读取列,我会得到它)
In the example below I'm trying to assign a value from one DataTable to another (all columns in the example are of type Int32 ). 在下面的示例中,我尝试将一个DataTable中的值分配给另一个(示例中的所有列都是Int32类型)。 I can assign a value to the 'newOrderRow.items' column but when I do the same with the 'newOrderRow.debcode' column an Exception is thrown!
我可以为'newOrderRow.items'列分配一个值,但是当我对'newOrderRow.debcode'列执行相同操作时,会抛出异常! Why?!
为什么?!
Some of the things I've tried so far (without any luck): 到目前为止我尝试过的一些事情(没有任何运气):
- Assign hard coded value instead of 'calclineRow.debcode' - 分配硬编码值而不是'calclineRow.debcode'
- Call newOrderRow.SetdebcodeNull() before assigning another value - 在分配另一个值之前调用newOrderRow.SetdebcodeNull()
- Changed DefaultValue property on 'debcode' column in 'orderrows' table from DBNull to -1 and it STILL throws the Exception and says it's DBNull !!! - 将'orderrows'表中'debcode'列的DefaultValue属性从DBNull更改为-1, 并且STILL抛出异常并说它是DBNull!
myDataSet.orderrowsRow newOrderRow;
foreach (MyDataSet.calclinesRow calclineRow in myDataSet.calclines)
{
newOrderRow = myDataSet.orderrows.NeworderrowsRow(); //Create new 'orderrows' row
//Assign values from one DataTable to another
if (!calclineRow.IsitemsNull())
newOrderRow.items = calclineRow.items; //calclineRow.items == 1. Assignment successful
if (!calclineRow.IsdebcodeNull())
newOrderRow.debcode = calclineRow.debcode; //calclineRow.debcode == 556. Assignment raises System.Data.StrongTypingException ! (See message below)
myDataSet.orderrows.AddorderrowsRow(newOrderRow);
}
/*Exception Message:
=====================
System.Data.StrongTypingException: The value for column 'debcode' in table 'orderrows' is DBNull.
---> System.InvalidCastException: Specified cast is not valid.
at MyProject.MyDataSet.orderrowsRow.get_debcode() in Q:\MyProjFolder\DataSets\MyDataSet.Designer.cs:line 21680
*/
You have to use the auto-generated SetNull
methods if the nullable property is null: 如果nullable属性为null,则必须使用自动生成的
SetNull
方法:
if (!calclineRow.IsitemsNull())
newOrderRow.items = calclineRow.items;
else
newOrderRow.SetitemsNull();
if (!calclineRow.IsdebcodeNull())
newOrderRow.debcode = calclineRow.debcode;
else
newOrderRow.SetdebcodeNull();
You also have to ad the new DataRow
to the table in the loop since NeworderrowsRow
does this not automatically. 您还必须将新的
DataRow
到循环中的表中,因为NeworderrowsRow
不会自动执行此操作。
myDataSet.orderrows.AddNeworderrowsRow(newOrderRow);
The line where the exception occurs(MyDataSet.Designer.cs:line 21680) suggests that it's raised from an auto-generated method of the DataSet
which reads this property. 发生异常的行(MyDataSet.Designer.cs:行21680)表明它是从
DataSet
的自动生成的方法引发的,该方法读取此属性。 Since you haven't used SetdebcodeNull
it does not know that it's null and throws the StrongTypingException
when it tries to read it. 由于您还没有使用
SetdebcodeNull
因此它不知道它为null并在尝试读取时抛出StrongTypingException
。
You could try this: 你可以试试这个:
if (!calclineRow.IsdebcodeNull())
newOrderRow["debcode"] = calclineRow.debcode;
While I admit it doesn't make a lot of sense, it appears as if calling the Set
for newOrderRow.debcode has the effect of calling the Get
, which as noted throws an exception if the underlying property is DbNull. 虽然我承认它没有多大意义,但似乎调用
Set
for newOrderRow.debcode具有调用Get
的效果,如上所述,如果底层属性为DbNull则抛出异常。
SOLVED. 解决了。 Sorry, my bad.
对不起这是我的错。
I forgot that I was doing things with the 'debcode' column in the OnColumnChanging event handler on my DataTable. 我忘记了我正在使用DataTable上的OnColumnChanging事件处理程序中的'debcode'列进行操作。 When I disabled that it all worked as it should.
当我禁用它时它一切正常。
Thanks anyway! 不管怎么说,还是要谢谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.