Reputation: 161
Can anyone please tell me why I get a StrongTypingException when ASSIGNING a value to a column in a strongly typed DataTable? (I understand why I get it if I were to READ the column with DBNull value)
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). 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! Why?!
Some of the things I've tried so far (without any luck):
- Assign hard coded value instead of 'calclineRow.debcode'
- Call newOrderRow.SetdebcodeNull() before assigning another value
- Changed DefaultValue property on 'debcode' column in 'orderrows' table from DBNull to -1 and it STILL throws the Exception and says it's 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
*/
Upvotes: 4
Views: 11496
Reputation: 161
SOLVED. Sorry, my bad.
I forgot that I was doing things with the 'debcode' column in the OnColumnChanging event handler on my DataTable. When I disabled that it all worked as it should.
Thanks anyway!
Upvotes: 0
Reputation: 460028
You have to use the auto-generated SetNull
methods if the nullable property is null:
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.
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. Since you haven't used SetdebcodeNull
it does not know that it's null and throws the StrongTypingException
when it tries to read it.
Upvotes: 4
Reputation: 13965
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.
Upvotes: 0