Add New Blank Row Into Existing DataTable While Looping Problem.

K

Kelvin

Hi All,

I wrote a program insert new row between rows while the datatable is
looping. It display the error message.
Exception Details: System.ArgumentException: This row already belongs
to this table.
[ArgumentException: This row already belongs to this table.]
System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID,
Int32 pos) +319
System.Data.DataRowCollection.Add(DataRow row) +14
ASP.Outstandrpt_aspx.CheckOrder(Object src, EventArgs e) in
c:\Inetpub\wwwroot\Outstandrpt.aspx:398
ASP.Outstandrpt_aspx.DetailGrid_setPage(Object src,
DataGridPageChangedEventArgs e) in
c:\Inetpub\wwwroot\Outstandrpt.aspx:118
System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs
e) +111
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source,
EventArgs e) +395
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +31
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e) +120
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +31
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
+122
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String
eventArgument) +288
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +5
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+166
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+5157

my code:

y = myTable.Rows.Count;

DataRow dr = myTable.NewRow();
DataRow mydr = myTable.NewRow();

for (i=0; y > i ; i++ )
{
DataRow dataRow = myTable.Rows;
// DataRow dataRow = myTable.NewRow();
if (myOrderNo == "")
{
myOrderNo = Convert.ToString(dataRow["OrderNo"]);
}
else
{
if (y == i+1)
{
dataRow["CustNo"] = "Sub Total :";
dataRow["HDelivSeq"] = "";
myTable.Rows.Add(dataRow);
}
}

if (myOrderNo != Convert.ToString(dataRow["OrderNo"]))
{
dataRow["CustNo"] = "Sub Total :";
dataRow["HDelivSeq"] = "";
dataRow["OrderNo"] = "";
myTable.Rows.Add(dataRow);
}
}
DetailGrid.DataSource = myTable;
 
L

Lowell Heddings

Here's the problem: You are creating the dataRow object and setting it
equal to a row that already exists right here:
DataRow dataRow = myTable.Rows;


And then you are attempting to add the row that already exists here:
myTable.Rows.Add(dataRow);

What you should be doing is using one of these variables that you've
created but not used:
DataRow dr = myTable.NewRow();
DataRow mydr = myTable.NewRow();

So in your loop, you need to do this:

DataRow dr = myTable.NewRow();
// do any processing here to the datarow
myTable.Rows.Add(dr);

The whole point of using NewRow() is to create a new row object that has
the same schema as the table. But you have to add that same object to
the table.

Lowell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top