DataAdapter.Update Parent->Childs Relation Problem
- From: Xusco <Xusco@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 14 Aug 2008 07:39:01 -0700
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;
}
.
- Prev by Date: Re: Getting Errors with Transaction
- Next by Date: Re: DataGrid will not display
- Previous by thread: Entity Framework: Removing tables from model
- Next by thread: GridView error checking
- Index(es):
Relevant Pages
|