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
Solved

Copy typed dataset content and update references accordingly

Posted on 2009-05-05
4
685 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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