Solved

Copy typed dataset content and update references accordingly

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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