Insert New Row into Existing DataTable Big Problem Again .. Please help !!!

  • Thread starter Thread starter Kelvin
  • Start date Start date
K

Kelvin

Hi All,

Please advised. Thanks alot.

y = myTable.Rows.Count;

for (i=0; y > i ; i++ )

{

DataRow dataRow = myTable.Rows; <----If using this syntax, Error
Message: Exception Details: System.ArgumentException: This row already
belongs to this table.

DataRow dataRow = new DataRow(); <---- If using this syntax, I can't
get table column in each row and can't insert row between rows. I have
to calculate total price while datatable looping


if (myOrderNo != (string)dataRow["OrderNo"]) <--- Checking Different
Order No Between Previous and Current Record.
{
dataRow["StyleSize"] = "";

dataRow["Stockroom"] = "";

dataRow["CurrencyCode"] = "";

dataRow["TotalPrice"] = mySubTotal;

myTable.Rows.Add(dataRow); <----- Error Here

myTable.AcceptChanges();

myOrderNo = (string)dataRow["OrderNo"];
}

}
 
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








Hi All,

Please advised. Thanks alot.

y = myTable.Rows.Count;

for (i=0; y > i ; i++ )

{

DataRow dataRow = myTable.Rows; <----If using this syntax, Error
Message: Exception Details: System.ArgumentException: This row already
belongs to this table.

DataRow dataRow = new DataRow(); <---- If using this syntax, I can't
get table column in each row and can't insert row between rows. I have
to calculate total price while datatable looping


if (myOrderNo != (string)dataRow["OrderNo"]) <--- Checking Different
Order No Between Previous and Current Record.
{
dataRow["StyleSize"] = "";

dataRow["Stockroom"] = "";

dataRow["CurrencyCode"] = "";

dataRow["TotalPrice"] = mySubTotal;

myTable.Rows.Add(dataRow); <----- Error Here

myTable.AcceptChanges();

myOrderNo = (string)dataRow["OrderNo"];
}

}
 
Lowell said:
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








Hi All,

Please advised. Thanks alot.

y = myTable.Rows.Count;

for (i=0; y > i ; i++ )

{

DataRow dataRow = myTable.Rows; <----If using this syntax, Error
Message: Exception Details: System.ArgumentException: This row already
belongs to this table.

DataRow dataRow = new DataRow(); <---- If using this syntax, I can't
get table column in each row and can't insert row between rows. I have
to calculate total price while datatable looping


if (myOrderNo != (string)dataRow["OrderNo"]) <--- Checking Different
Order No Between Previous and Current Record.
{
dataRow["StyleSize"] = "";

dataRow["Stockroom"] = "";

dataRow["CurrencyCode"] = "";

dataRow["TotalPrice"] = mySubTotal;

myTable.Rows.Add(dataRow); <----- Error Here

myTable.AcceptChanges();

myOrderNo = (string)dataRow["OrderNo"];
}

}
 
DataRow dr = myTable.NewRow(); > DataRow mydr = myTable.NewRow();
<----- Not Uses


So in your loop, you need to do this:
DataRow dr = myTable.NewRow();

// do any processing here to the datarow

myTable.Rows.Add(dr);

Is it correct the following code ?

y = myTable.Rows.Count;
for (i=1; y > i ; i++ )
{
DataRow dr = myTable.NewRow();

DataRow dataRow = myTable.Rows;
if (myOrderNo != (string)dataRow["OrderNo"])
{
dr["OrderNo"] = "";
dr["TotalPrice"] = mySubTotal;
myTable.Rows.Add(dr);
myTable.AcceptChanges();
myOrderNo = (string)dataRow["OrderNo"];

}
}
 
This looks more correct, I'd have to see the entire code to know, but I
would imagine that should work a little better.

You might have to wait until after the end of the loop to do the
AcceptChanges(), I haven't tested that.

Lowell



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

<----- Not Uses


So in your loop, you need to do this:
DataRow dr = myTable.NewRow();

// do any processing here to the datarow

myTable.Rows.Add(dr);

Is it correct the following code ?

y = myTable.Rows.Count;
for (i=1; y > i ; i++ )
{
DataRow dr = myTable.NewRow();

DataRow dataRow = myTable.Rows;
if (myOrderNo != (string)dataRow["OrderNo"])
{
dr["OrderNo"] = "";
dr["TotalPrice"] = mySubTotal;
myTable.Rows.Add(dr);
myTable.AcceptChanges();
myOrderNo = (string)dataRow["OrderNo"];

}
}
 
Hi Lowell,

Basically, It is ok, Thanks, But I don't why put all new row can't
located row by row. All new rows move into buttom. Can't I do that
if (myOrderNo != (string)dataRow["OrderNo"]) then Insert New Row ?
For example.
Order No Total Price
111111 2000
111111 1000
 
Hi Lowell,

Basically, It is ok, Thanks, But I don't why put all new row can't
located row by row. All new rows move into buttom. Can't I do that
if (myOrderNo != (string)dataRow["OrderNo"]) then Insert New Row ?
For example.
Order No Total Price
111111 2000
111111 1000
-------------------------------------------------
Subtotal 3000

222222 500
222222 600

I imagine you can change the logic somewhat, but the key thing to
remember is that you need to use the Table.NewRow function to create the
new datarow object before you add it to the table.

Lowell
 
Hi Lowell,

Thanks alot first !
If add the following code, it will insert new row immediately before
read next record, right ?

dr["OrderNo"] = "";
dr["TotalPrice"] = mySubTotal;
myTable.Rows.Add(dr);
 
Hi Lowell,

Thanks alot first !
If add the following code, it will insert new row immediately before
read next record, right ?

dr["OrderNo"] = "";
dr["TotalPrice"] = mySubTotal;
myTable.Rows.Add(dr);

It will insert the row, but the row will be at the end of the datatable.
If you need to insert in the middle, you would use myTable.Rows.InsertAt().

Again, I'm not sure how that works when you are looping through.
Probably a better way of doing it would be to make those changes to a
copy of the dataset.

Lowell
 
Hi Lowell,
I check it , I think it can't insert between each rows. Please advise.

Then you are going to want to insert the rows into a copy of the
datatable during your processing.

Lowell
 
Hi Lowell

Yes. I want to insert new row and all records into new datatable. but I
don't know how does it work ? Please advise. Thanks
 
DataRow dataRow = new DataRow();

You cannot instantiate the DataRow class except through the NewRow
method on the DataTable.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top