Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Inserting relational data - datagrid error - it's not picking up primary key of parent

Avatar of myxiplx
myxiplx asked on
Visual Basic.NET
20 Comments1 Solution392 ViewsLast Modified:
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.


Avatar of vbturbo
vbturboFlag of Denmark image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answers