How do I use a CurrencyManager object with Relational Data?

  • Thread starter Thread starter David Adams
  • Start date Start date
Hi,

I have a strongly typed dataset with 15-20 tables which have typed
relationships such as:

Customers-Orders
Orders-OrderDetails
OrderDetails-ProductDetails
etc...

I am using auto-increment values for every record that is added, and
replacing them with an SQL IDENTITY() value. This all works fine except for
when it comes to adding a child record via a currency manager. The parent
"id" does not seem to propagate to the foreign key child column.

I have a form (WinForms app) for each of these tables with a datagrid
showing each table, which contains a user control, which is nothing more
than an add, update, delete record editor. I pass a currencymanager to the
user control such as: new uc(this.BindingContext[ds,"OrderDetails"]). I
have also experimented with passing the datarelation to the control such as:
this.BindingContext[ds,"Orders.OrdersOrderDetails"]). The problem with this
method is that when the changes are submitted to the database, my
currencymanager goes beserk and has the position set to -1, clearing the
grids and basically having everything go "blank".

I don't know what to do at this point. There seem to be only "easy"
examples of using a currencymanager that I have found. If someone could
tell me the proper way to set up a currencymanager when dealing with related
data, I would appreciate it. The grids appear on tabs of a tab control, if
that makes a difference.

Any suggestions would be appreciated!

Thanks,
Dave
 
Two thoughts...

1. Only update one master record at a time -- this will
make it easier to discover what the identity field value
is that will be propagated to the detail record(s).

2. Use the Row_Updated event of the data adapter to catch
the changes being made to the detail records and update
that row with the "discovered" identity field value of the
master record.

Example code for Row_Update event to find the identity
value in the master record and update that record ...

....
Dim cmdGetIdentity As New OleDbCommand( _
"SELECT @@IDENTITY", da.SelectCommand.Connection)

If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row(idFieldName) = CInt(cmdGetIdentity.ExecuteScalar)
e.Row.AcceptChanges()
End If
....

Note: save the identity field value from the master
record in a global somewhere so that you can propogate it
to the detail records.
 
Back
Top