0

I have two tables that are pretty much exact clones of one another (identical columns, just different columns set as primary keys). Basically the second table is just for keeping a history of the first table. What I need to do is, when a user updates a record in table 1 I need to insert the original copy of that record into table 2.

I am using a LinqDataSource object and utilizing the LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e) event so I have access to e.OriginalObject and that will be perfect for inserting the original row in table 2. My problem is that I don't want to have to set every property manually because there are about 50 of them, so I want to use Reflection but am not sure how to properly go about it.

Consider the following code:

INSTRUMENT_DATA_SHEET _original = (INSTRUMENT_DATA_SHEET)e.OriginalObject; INSTRUMENT_DATA_SHEET_HISTORY _history = new INSTRUMENT_DATA_SHEET_HISTORY(); 

How can I go about copying all of the _original's property values to _history's? I have tried using the solution from this question, however it isn't working for me. It throws the error:

Property DATE has an incompatible type in E_and_I.INSTRUMENT_DATA_SHEET_HISTORY

My guess is that it's because the DATE column is part of the primary key in table 2, but not table 1. As I said, the only difference between the two tables are the primary keys. Here they are for your reference:

Primary Keys

2
  • Have you considered doing this using SQL triggers instead of in code? Commented Aug 8, 2012 at 16:07
  • 1
    I'd rather do it in the code :) Commented Aug 8, 2012 at 16:08

2 Answers 2

1

The problem I see is that your History type Date field is DateTime and your Original one is DateTime? (same problem with REV in History, it can't be null). You'll have to decide what happens if there is a null DateTime of Date in your original version. Then you should be able to modify Skeets code (oh dear!) to handle specifically these fields differently then the rest of the fields.

Sign up to request clarification or add additional context in comments.

3 Comments

That's precisely what I'm trying to do actually - haven't figured it out yet though...
@Hoff I think you should make the columns in the source table not null as well, because apparently it is a requirement: you can't create a history record is one of the values is null.
Agreed - gotta love getting slapped with somebody else's database (lazy & unthought out) design.
0

Ok I've managed to figure it out :) Here's what I did:

INSTRUMENT_DATA_SHEET _original = (INSTRUMENT_DATA_SHEET)e.OriginalObject; INSTRUMENT_DATA_SHEET_HISTORY _history = new INSTRUMENT_DATA_SHEET_HISTORY(); foreach (PropertyInfo pi in _original.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { _history.GetType().GetProperty(pi.Name).SetValue(_history, pi.GetValue(_original, null), null); } 

Not very elegant but it gets the job done!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.