Dataset, datatable. datarow replication method -- Solution: This row already belongs to another table

Source: Internet
Author: User

Recently, the project was too busy and there was almost no time to patronize it.
In addition to implementing its own modules, the project also has a team leader which is not competent.

Because the module is responsible for a lot of data operations, this error is often encountered, "This row already belongs to another table" error.

 

The statement that causes this error is as follows:

Dtpricetable. Rows. insertat (adatarow, I );

Or

Dtpricetable. Rows. Add (adatarow );

 

I analyzed the cause because datarow datatable is called by reference. Therefore, if a row is in one table, it cannot be added to another table.

 

There are two specific improvement strategies:

1. assign values to each column of the original datarow. As follows:

Datarow temprow = dtitemprice. newrow ();

Temprow ["first"] = adatarow ["first"]

Temprow ["second"] = adatarow ["second"]

Temprow ["third"] = adatarow ["third"]

Temprow ["forth"] = adatarow ["forth"]

Temprow ["th"] = adatarow ["th"]

......

 

Dtpricetable. Rows. Add (temprow)

2. A colleague reminded me why every column of the able is not cyclically stored? I guess so. Why not!

Well, no matter how many columns are in the table, you only need three sentences.

Datarow adatarow = dtitemprice. newrow ();

Foreach (datacolumn adatacolumn in dtitemprice. columns)

{

Adatarow [adatacolumn. columnname] = dritemprices [I] [adatacolumn. columnname];

}

Insertconditiontype (adatarow, dtitemprice );

 

Test.

Later, I checked that both dataset and able have a method: Copy () to copy the structure and data of the system. Data. datatable.

 

Then, using this function, both dataset, able, and datarow can be copied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.