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
*/
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!