Please help me understand

  • Thread starter Thread starter Hardin
  • Start date Start date
H

Hardin

I have a typed dataset with 3 datatables from Northwind: Customers, Orders,
and Order Details with the appropriate relations and cascade updates and
deletes set in the dataset.

I have a SQLDataAdapter for each table, with Wizard-generated stored
procedures used for Select, Update, Insert, and Delete.

At update time, I use GetChanges to get a new dataset, return that dataset
to a middle-layer, and run the following code (the dataset of just changed
data is called "ds":) based on code in David Sceppa's book:


//Update order: Deletes, Updates, Inserts
try
{
// Deletes go from bottom up
daDetails.Update(ds.Tables["Order Details"].Select("", "",
DataViewRowState.Deleted));
daOrders.Update(ds.Tables["Orders"].Select("", "",
DataViewRowState.Deleted));
daCust.Update(ds.Tables["Customers"].Select("", "",
DataViewRowState.Deleted));

// Updates go from top down
daCust.Update(ds.Tables["Customers"].Select("", "",
DataViewRowState.ModifiedCurrent));
daOrders.Update(ds.Tables["Orders"].Select("", "",
DataViewRowState.ModifiedCurrent));
daDetails.Update(ds.Tables["Order Details"].Select("", "",
DataViewRowState.ModifiedCurrent));

//Inserts go from top down
daCust.Update(ds.Tables["Customers"].Select("", "",
DataViewRowState.Added));
daOrders.Update(ds.Tables["Orders"].Select("", "",
DataViewRowState.Added));
daDetails.Update(ds.Tables["Order Details"].Select("", "",
DataViewRowState.Added));
}
.....

First, there are no exceptions thrown during this process.

However, the update process appears to be incomplete, and it seems to be
caused by a problem with the DataAdapter.Update method. After one of the
statements above executes, the dataAdapter (according to MS's documentation)
calls AcceptChanges. If it called AcceptChanges just on each row as it was
sent to the SQL StoredProcedure, that would be fine. But instead, it seems
to either be calling it on the entire dataset or else AcceptChanges is
bubbling down the relations change in the dataset. In any case, if there
are changes or updates to more than one table, this routine only updates one
but marks all pending changes as unchanged.

Any suggestions, code samples, property settings, etc., that anyone can
suggest to make this work?

Thanks a lot,
-- Hardin
 
Hi Hardin,

If it is for learning I don't have an answer, if it is for using, take a
look for SQLcommandbuilder or Oledbcommandbuilder.

Saves you a lot of work.

Cor
 
Hi Hardin,

A dataset will cascade the AcceptChanges() and RejectChanges() through
DataRelations by default. However, you can change this behavior by set the
Accept/ Reject Rule to None when designing the DataRelation in the Edit
Relation dialog box. You can also change it in code. I've written a code
snippet to achieve this: (Since casting Constraints to ForiegnKeyConstraint
is not safe, we have to check it first.)

if(this.dataset11.Orders.Constraints["CustomersOrders"] is
ForeignKeyConstraint)
{
fkc =
(ForeignKeyConstraint)this.dataset11.Orders.Constraints["CustomersOrders"];
fkc.AcceptRejectRule = AcceptRejectRule.None;
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Reply-To: "Hardin" <[email protected]>
| From: "Hardin" <[email protected]>
| Subject: Please help me understand
| Date: Mon, 3 Nov 2003 15:12:43 -0700
| Lines: 60
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: sdn-ap-031caburbp0431.dialsprint.net 65.179.129.177
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:65280
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I have a typed dataset with 3 datatables from Northwind: Customers,
Orders,
| and Order Details with the appropriate relations and cascade updates and
| deletes set in the dataset.
|
| I have a SQLDataAdapter for each table, with Wizard-generated stored
| procedures used for Select, Update, Insert, and Delete.
|
| At update time, I use GetChanges to get a new dataset, return that dataset
| to a middle-layer, and run the following code (the dataset of just changed
| data is called "ds":) based on code in David Sceppa's book:
|
|
| //Update order: Deletes, Updates, Inserts
| try
| {
| // Deletes go from bottom up
| daDetails.Update(ds.Tables["Order Details"].Select("", "",
| DataViewRowState.Deleted));
| daOrders.Update(ds.Tables["Orders"].Select("", "",
| DataViewRowState.Deleted));
| daCust.Update(ds.Tables["Customers"].Select("", "",
| DataViewRowState.Deleted));
|
| // Updates go from top down
| daCust.Update(ds.Tables["Customers"].Select("", "",
| DataViewRowState.ModifiedCurrent));
| daOrders.Update(ds.Tables["Orders"].Select("", "",
| DataViewRowState.ModifiedCurrent));
| daDetails.Update(ds.Tables["Order Details"].Select("", "",
| DataViewRowState.ModifiedCurrent));
|
| //Inserts go from top down
| daCust.Update(ds.Tables["Customers"].Select("", "",
| DataViewRowState.Added));
| daOrders.Update(ds.Tables["Orders"].Select("", "",
| DataViewRowState.Added));
| daDetails.Update(ds.Tables["Order Details"].Select("", "",
| DataViewRowState.Added));
| }
| ....
|
| First, there are no exceptions thrown during this process.
|
| However, the update process appears to be incomplete, and it seems to be
| caused by a problem with the DataAdapter.Update method. After one of the
| statements above executes, the dataAdapter (according to MS's
documentation)
| calls AcceptChanges. If it called AcceptChanges just on each row as it
was
| sent to the SQL StoredProcedure, that would be fine. But instead, it
seems
| to either be calling it on the entire dataset or else AcceptChanges is
| bubbling down the relations change in the dataset. In any case, if there
| are changes or updates to more than one table, this routine only updates
one
| but marks all pending changes as unchanged.
|
| Any suggestions, code samples, property settings, etc., that anyone can
| suggest to make this work?
|
| Thanks a lot,
| -- Hardin
|
|
|
 
Actually, there's an error in the documentation regarding
that behavior.

When you submit the pending change in a DataRow by calling
DataAdapter.Update, the DataAdapter will implicitly call the
DataRow's AcceptChanges method if it successfully submitted the
pending change stored in the row.

If you have a DataRelation that points to child rows, this
implicit call to AcceptChanges will cascade down to the related
child rows if the DataRelation's ForeignKeyConstraint's
AcceptRejectRule property is set to Cascade. There's an error in
the documentation that states that this property is set to
Cascade by default. The actual default is None, which means that
the call should not cascade down to the related child rows. Some
developers have seen that error in the documentation and have set
the AcceptRejectRule property to Cascade.

Hardin, please check this property in your code. If it's
set to Cascade, set it to None instead. If this does not explain
the behavior and resolve your problem, please let us know.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
Thank you, David.
That was indeed the problem -- the AcceptRejectRule was set to Cascade.
However, I'm pretty sure I didn't set it that way (I know I didn't set it
that way on purpose, in any case, but perhaps I fumbled with the XSD
relations designer/wizard).

Thank you for the reply.
-- Hardin
 
Back
Top