?
Solved

Copy typed dataset content and update references accordingly

Posted on 2009-05-05
4
Medium Priority
?
690 Views
Last Modified: 2013-12-17
Hi,

The application I am working on is using typed datasets as business objects.
Now I need to create new object based on existing one.
The dataset contain 3 datatables, let say
 - Master
      MasterID
      Name

 - Data
      DataID
      MasterID
      SomeData

 - Layout
      LayoutID
      MasterID
      DataID
      SomeOtherData


So there is kind of three-way relationship exists: Each Layout record has reference to one Data record and both Data and Layout records reffering to the Master Record

My problem is to create a copy of the dataset (all 3 tables) with Layout records referring to NEWLY created Data records not to old ones.

Any idea how to do this?

P.S. The application is build with VS 2005 (.Net framework 2.0)
0
Comment
Question by:Michael_D
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24311465
Probably you should use a "clone" method. Do you have details about the class that implement this business objects ?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24312195
The description is not very clear. Its clear about creating a copy but what you mean it should refer to newly created data objects?
0
 
LVL 13

Author Comment

by:Michael_D
ID: 24314875
>>> kaylanreilor
Clone or Copy methods will create exact copy of the object and will not change ID fields which will cause duplication errors in database since the ID fields should be unique.
The implementation is just generated dataSet object.

>>> CodeCruiser
here is a sample: let say I have this data

- Master
      MasterID      Name
      1                 Master 1

 - Data
      DataID      MasterID      SomeData
      1               1                 Data 1
      2               1                 Data 2

 - Layout
      LayoutID      MasterID      DataID      SomeOtherData
      1                 1                  1              Layout 1
      2                 1                  2              Layout 2
      3                 1                  2              Layout 3
   
Note: the Layout.DataID is FK into Data table and it could be more then one Layout record associated with one Layout Record

All Id fields are auto-increment fields

After copy I want have new record as following

- Master
      MasterID      Name
      2                 Copy of Master 1

 - Data
      DataID      MasterID      SomeData
      3               2                 Data 1
      4               2                 Data 2

 - Layout
      LayoutID      MasterID      DataID      SomeOtherData
      4                 2                  3              Layout 1
      5                 2                  4              Layout 2
      6                 2                  4              Layout 3

As you can see new records for Data and Layout are pointing now to MasterId= 2 (new record)
But this is not an issue as I can create copy of Master first, grab the new Id and then in loop create copy of Data records replacing MasterId with new Id.
The problem is how to create Layout records that linked to correct Data record (to the new one)

I hope this is clarify my intention.

0
 
LVL 13

Accepted Solution

by:
Michael_D earned 0 total points
ID: 24316898
Ok I found a solution.

I am first looping through Data rows and insert new records into DB. The new Ids is stored in the dictionary with oldId as a key.
When I am looping through layout rows I replace the DataId with the value from dictionary.

Kind of silly but it works.

Thanks for participating
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question