Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Adding DataRow to DataTable

Adding DataRow to DataTable

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zuhx
    wrote on last edited by
    #1

    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); }

    C 1 Reply Last reply
    0
    • Z zuhx

      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); }

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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#

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups