add row to child table

hanzie

Member
Joined
Mar 19, 2008
Messages
7
Programming Experience
Beginner
Hello,

I have a database with 3 tables: Customer, Order, Orderdetails.
The tables a related to each other with:
Customer.customerID, Order.customerID, Orderdetails.OrderID

With a selectquery I filled a dataset with data from these tables.
De data is now in a new dataset-table "NewOrders".

What I now wanna do is add a new row to the database-table Orderdetails.

The new row must have the same values as in the dataset-table "NewOrders" except it must get a new value for the OrderDate-column. The date is selected with a datetimepicker.


I have for example the following code:

VB.NET:
'Fill the DataSet using the SqlDataAdapter  
objdataAdapter.Fill(objDataSetOrders, "NewOrders")

'Add new Rows
Dim objdatarow As DataRow
        objdatarow = objDataSetOrders.Tables("NewOrders").NewRow
        objdatarow("CustomerID") = objDataSetOrders.Tables("NewOrders").Rows(0).Item("CustomerID")
        objdatarow("CustomerName") = objDataSetOrders.Tables("NewOrders").Rows(0).Item("CustomerName")
        objdatarow("OrderID") = objDataSetOrders.Tables("NewOrders").Rows(0).Item("OrderID")
        objdatarow("OrderName") = objDataSetOrders.Tables("NewOrders").Rows(0).Item("OrderName")
        objdatarow("OrderDate") = DateTimePicker1.value.Date.ToShortDateString

 'add datarow
        objDataSetOrders.Tables("NewOrders").Rows.Add(objdatarow)

        'update dataset-table
        objdataAdapter.Update(objDataSetOrders.Tables("NewOrders"))

But i get the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customer". The conflict occurred in database "OrderData", table "dbo.Customer", column 'CustomerID'.

What do I have te change?
Trying for days now!!
 
Looks like you are missing this:

objdatarow("FK_Orders_Customer") = objDataSetOrders.Tables("NewOrders").Rows(0).Item("FK_Orders_Customer")

When you have a PK -> FK relationship the FK can not be empty.

The FK_Orders_Customer field needs to be there even if you do not allow it to be viewed
 
Read the DW2 link in my sig, first read "Creating a Simple Data App" and then read Displaying Related Data, and then any other related data sections
 

Latest posts

Back
Top