Adding DataRow to DataTable
-
I am trying to add an arraylist to a datatable. The ArrayList contains a struct and the the struct basically holds several object variables. I use a foreach statement to loop through the arraylist and add the data to a datarow then add the datarow to a datatable. However, when I try to add the second datarow I get an error message saying 'This row already belongs to this table'. Even though I know the data has changed the it is a completely different row. I am including my code snippet, any suggestions is appreciated: r = DataSet.Tables["TradeOrderFills"].NewRow(); theData = (ArrayList) oFills.GetByIndex(0); Console.WriteLine("theData count "+theData.Count); foreach(TradeData td in theData) { Console.WriteLine("Adding Row to DataTable"); Console.WriteLine(td.ticker); r["preTicker"] = (string)td.ticker; Console.WriteLine("column value "+r["preTicker"]); Console.WriteLine(td.transactionType); r["preTransType"] = (string)td.transactionType; Console.WriteLine("column value "+r["preTransType"]); r["preOrderTime"] = (string)td.orderTime; r["prePrice"] = Convert.ToSingle(td.price); r["preQ"] = Convert.ToSingle(td.fillQ); r["preAccount"] = (string)td.account; r["preBroker"] = (string)td.broker; r["preStatus"] = (string)td.status; DataSet.Tables["TradeOrderFills"].Rows.Add(r); //THIS IS WHERE THE ERROR OCCURS ON THE 2nd ITERATION Console.WriteLine("Number of Rows "+DataSet.Tables["TradeOrderFills"].Rows.Count); }
-
I am trying to add an arraylist to a datatable. The ArrayList contains a struct and the the struct basically holds several object variables. I use a foreach statement to loop through the arraylist and add the data to a datarow then add the datarow to a datatable. However, when I try to add the second datarow I get an error message saying 'This row already belongs to this table'. Even though I know the data has changed the it is a completely different row. I am including my code snippet, any suggestions is appreciated: r = DataSet.Tables["TradeOrderFills"].NewRow(); theData = (ArrayList) oFills.GetByIndex(0); Console.WriteLine("theData count "+theData.Count); foreach(TradeData td in theData) { Console.WriteLine("Adding Row to DataTable"); Console.WriteLine(td.ticker); r["preTicker"] = (string)td.ticker; Console.WriteLine("column value "+r["preTicker"]); Console.WriteLine(td.transactionType); r["preTransType"] = (string)td.transactionType; Console.WriteLine("column value "+r["preTransType"]); r["preOrderTime"] = (string)td.orderTime; r["prePrice"] = Convert.ToSingle(td.price); r["preQ"] = Convert.ToSingle(td.fillQ); r["preAccount"] = (string)td.account; r["preBroker"] = (string)td.broker; r["preStatus"] = (string)td.status; DataSet.Tables["TradeOrderFills"].Rows.Add(r); //THIS IS WHERE THE ERROR OCCURS ON THE 2nd ITERATION Console.WriteLine("Number of Rows "+DataSet.Tables["TradeOrderFills"].Rows.Count); }
Move your first line:
r = DataSet.Tables["TradeOrderFills"].NewRow();
inside the foreach loop. What you were doing is inserting the same physical row each time, but just changing the data it contained. You need to create a new physical row on each iteration. --Colin Mackay--"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#