DataAdapter.Update Parent->Childs Relation Problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello,

I have a DataSet with five Tables inside. Two of them have a Parent->Child
relation. When I try to update the the child Table it throws an Exception:
System.Data.VersionNotFound in the SqlRowUpdatedEventHandler. If I ommit this
line:

daGroupsAffiliateClient.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;


then it throws another exception saying that the Relation doesn't exist. The
code is very similar to this: http://support.microsoft.com/kb/320301. Can
anyone help me? Thanks a lot!!

Here is the code:

public dsAffiliateClient SaveAffiliateClient(dsAffiliateClient ds, string
connectionString)

{

Database db = new GenericDatabase(connectionString,
SqlClientFactory.Instance);

DbDataAdapter daGroupsAffiliateClient =
GetGroupsAffiliateClientDataAdapter(db);

DbDataAdapter daGarmentGroupAffiliate =
GetGarmentGroupAffiliateDataAdapter(db);

DbDataAdapter daWardrobe = GetWardrobeDataAdapter(db);

using (DbConnection connection = db.CreateConnection())

{

connection.Open();

DbTransaction transaction = connection.BeginTransaction();

#region Set Connection and Transaction to Commands

daGroupsAffiliateClient.UpdateCommand.Connection = connection;

daGroupsAffiliateClient.UpdateCommand.Transaction = transaction;

daGroupsAffiliateClient.InsertCommand.Connection = connection;

daGroupsAffiliateClient.InsertCommand.Transaction = transaction;

daGroupsAffiliateClient.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;




daGarmentGroupAffiliate.UpdateCommand.Connection = connection;

daGarmentGroupAffiliate.UpdateCommand.Transaction = transaction;

daGarmentGroupAffiliate.InsertCommand.Connection = connection;

daGarmentGroupAffiliate.InsertCommand.Transaction = transaction;

daWardrobe.UpdateCommand.Connection = connection;

daWardrobe.UpdateCommand.Transaction = transaction;

daWardrobe.InsertCommand.Connection = connection;

daWardrobe.InsertCommand.Transaction = transaction;

#endregion




try

{

((SqlDataAdapter)daGroupsAffiliateClient).RowUpdated += new
SqlRowUpdatedEventHandler(OnGroupsAffiliateClientRowUpdated);

((SqlDataAdapter)daGarmentGroupAffiliate).RowUpdated += new
SqlRowUpdatedEventHandler(OnGarmentGroupAffiliateRowUpdated);

//daWardrobe.Update(ds, "Wardrobe");


DataRow[] dsArray = ds.Tables["GroupsAffiliateClient"].Select("", "",
DataViewRowState.ModifiedCurrent);

daGroupsAffiliateClient.Update(ds.Tables["GroupsAffiliateClient"].Select("",
"", DataViewRowState.ModifiedCurrent));

daGarmentGroupAffiliate.Update(ds.Tables["GarmentGroupAffiliate"].Select("",
"", DataViewRowState.ModifiedCurrent));

daGroupsAffiliateClient.Update(ds.Tables["GroupsAffiliateClient"].Select("",
"", DataViewRowState.Added));

ds.EnforceConstraints = false;

daGarmentGroupAffiliate.Update(ds.Tables["GarmentGroupAffiliate"].Select("",
"", DataViewRowState.Added));

ds.EnforceConstraints = true;

transaction.Commit();

}

catch (Exception ex)

{

transaction.Rollback();

throw ex;

}

finally

{

if (connection.State == ConnectionState.Open)

{

connection.Close();

}

}

}

ds.AcceptChanges();

return ds;

}

protected static void OnGroupsAffiliateClientRowUpdated(object sender,
SqlRowUpdatedEventArgs args)

{

if (args.StatementType == StatementType.Insert || args.StatementType ==
StatementType.Delete)

args.Status = UpdateStatus.SkipCurrentRow;

}

protected static void OnGarmentGroupAffiliateRowUpdated(object sender,
SqlRowUpdatedEventArgs args)

{

if (args.StatementType == StatementType.Insert)

{

// Do not allow the AcceptChanges to occur on this row.

args.Status = UpdateStatus.SkipCurrentRow;

// Get the current, actual primary key value so that you can plug it back

// in after you get the correct original value that was generated for the
child row.

int currentkey = (int)args.Row["GroupAffiliateClientId"];

// This is where you get a correct original value key that is stored to the
child row.

// You pull the original, pseudo key value from the parent, plug it in as
the child row's primary key

// field, and then accept changes on it. Specifically, this is why you
turned off EnforceConstraints.

DataRelationCollection drc = args.Row.Table.ParentRelations;

args.Row["GroupAffiliateClientId"] =
args.Row.GetParentRow("FK_GarmentGroupAffiliate_GroupsAffiliateClient")["GroupAffiliateClientId", DataRowVersion.Original];

args.Row.AcceptChanges();

// Store the actual primary key value in the foreign key column of the child
row.

args.Row["GroupAffiliateClientId"] = currentkey;

}

if (args.StatementType == StatementType.Delete)

args.Status = UpdateStatus.SkipCurrentRow;

}

.



Relevant Pages

  • Re: Classic Nest SP with Transaction Question
    ... This will take care of what is done in the child, but what I want to do is ... rollback the entire outer transaction - the one initiated in the outer SP. ...
    (microsoft.public.sqlserver.programming)
  • Re: Working Transactions somehow started not to work
    ... Transaction and locking are not properties of recordsets but are properties ... Opening a served-based cursor will put a lock on ... If a connection is closed, ... SL> and are not associated necessarily with server-based cursors. ...
    (microsoft.public.access.adp.sqlserver)
  • Setting Locks on Rows in MSSQL Database (Currenlty Unsolvable Problem and Trying)
    ... I am beginning a transaction from PHP and setting a lock on a record ... I then commit the transaction I started when the user clicked ... There is no way in PHP where I can start a persistent ms sql connection ...
    (alt.php)
  • Re: Classic Nest SP with Transaction Question
    ... enclosing transaction or should begin and commit/rollback its own ... > I echo Ron's points and also have been using an approach very similar to> Tom's method of using a SAVE PT instead of starting a new TRANSACTION if the> ChildSP is called from the ParentSP. ... > The ParentSp now has to ROLLBACK everything up to that point. ... As Ron> mentioned, the ParentSP may have called many child SPs, and performed many> updates up to this point, and all of these need to be rolled back. ...
    (microsoft.public.sqlserver.programming)
  • Re: Classic Nest SP with Transaction Question
    ... This will take care of what is done in the child, but what I want to do is ... rollback the entire outer transaction - the one initiated in the outer SP. ...
    (microsoft.public.sqlserver.programming)