I'm creating an order entry form from scratch with very little experience in .NET 2003 so this may well be an obvious error ;-)
The idea is to have a simple order entry form: The user enters the main details for the order (date required, etc), then on the same form adds in all the items they would like to order and the quantities.
My order database essentially consists of two tables: Orders and OrderDetails. Orders has an auto-generated primary key (Order_ID) and there's a straighforward one-to-many relationship between the two tables in the SQL database using that key. The first part of my form which saves the entries in the orders table is now done and fully working, where I'm struggling is when I add a datagrid to allow the user to add the order details.
The datagrid just displays two fields for the user: ItemName and Quantity.
My first attempt used two separate dataadaptors and two datasets. This allowed the user to fill in the order details and as many items as they liked and saved fine except that the Order_ID field was not filled in for the detail items (which I expected about but this is the first time I've used a datagrid, I was happy to just get it saving something as a first step).
I attempted to fill in the Order_ID manually but quickly realised this would mean stepping through all the detail items the user has entered and completing it for each one. I can do this easily enough but I'm sure there's a better way.
Reading about this on the net I discovered that you can define relationships between tables in a dataset. That has the advantage of making the form very easy to use for viewing data, so I now have the same two dataadapters but one dataset containing both tables, and in the code I'm defining the relationship between the tables (which I called OrderDetails).
Now that I've done that as soon as I enter any data in the datagrid I get this error:
"Error when committing the row to the original data store.
ForeignKeyConstraint OrderDetails requires the child key values (0) to exist in the parent table. Do you want to correct the value?"
I can see that the datagrid isn't picking up a value for the primary key of the Order table, but with this application working with an offline dataset I'm not sure this isn't expected. Surely the primary key won't be known until the application commits the order to the database?
I know that when I save the data to the database I need to call the dataadapter update statements in order: firstly for the order, and then for the order details table, but I'm not even getting that far before I get this error.
Can someone tell me where I'm going wrong. Please ask if I've not given enough information.