1

RejectChanges doesn't rollback the autonumber (I understand why thanks to this post). But, when I save a row. It overrides the autonumber which was originally returned to me. I need to know the ID of the parent row so I can create the related records.

Perhaps there is a better way I should be creating the related rows?

I saw this post which recommended changing the AutoIncrement to -1 and AutoSeed to -1. But, that does't make sense nor did it work for me.

In my code there are two tables, Shipments and Shipment_Package_Details. It's a 1-many relationship.

                r = this.allertDataSet3.Shipments.NewShipmentsRow();
                log.Debug("The new ShipmentRow ID is " + r.ShipmentID); // ID == 1
                this.allertDataSet3.Shipments.RejectChanges();
                r = this.allertDataSet3.Shipments.NewShipmentsRow();
                log.Debug("The new ShipmentRow ID is " + r.ShipmentID); // ID == 2
                r.NumPkgs = p.Length;
                this.allertDataSet3.Shipments.Rows.Add(r);

                // Create Related Row
                MyCompany.MyNamespace.allertDataSet3.Shipment_Package_DetailsRow pd = this.allertDataSet3.Shipment_Package_Details.NewShipment_Package_DetailsRow();
                pd.ShipmentID = r.ShipmentID;  // ID == 2 here.
                pd.TrackingNumber = trackingNumbers[i];
                this.allertDataSet3.Shipment_Package_Details.Rows.Add(pd);

                this.shipmentsTableAdapter.Update(this.allertDataSet3);
                // At this point if i set a breakpoint and look at database, it's been committed to the database with ID == 1. 

                // The following line will fail since I set the package details row to ID == 2.
                this.shipment_Package_DetailsTableAdapter.Update(this.allertDataSet3);
Community
  • 1
  • 1
blak3r
  • 16,066
  • 16
  • 78
  • 98
  • You absolutely need to set those two values to -1 regardless of anything else - it prevents the system confusing "new" records with existing records. It may not be all of the solution, but your system will break in other interesting ways if you haven't done that (I'm looking for my old .NET & Access code, but it may be gone...) – Murph Dec 21 '11 at 20:09
  • @Murph Hmm... I couldn't find very good documentation on why that is. My application doesn't have multiple users so maybe that's why it hasn't become an issue for me yet. – blak3r Dec 21 '11 at 23:38
  • I do remember having serious grief with even single user testing - but 10 years ago, which is why no proper answer (yet) – Murph Dec 22 '11 at 10:37

0 Answers0