By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Add New Blank Row Into Existing DataTable While Looping Problem.

P: n/a
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.OnPageIndexChan ged(DataGridPageChangedEventArgs
e) +111
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source,
EventArgs e) +395
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +31
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e) +120
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +31
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)
+122
System.Web.UI.WebControls.LinkButton.RaisePostBack Event(String
eventArgument) +288
System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +5
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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[i];
// 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;
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[i];
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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.