A
Aziz
Right...I have an access Database with around 10 tables. I have
modelled only a few of these in the XML Schema (enough to get my
program working) and assigned the relevant DataRelations.
Anyway I'm having problems with a three table
Order-OrderProducst-Product relationship which stores the temporary
order information in a Shopping Basket datatable.
ORDER
OrderID (PK) (Autonum)
ORDERPRODUCTS
OrderProductsID (PK) (Autonum)
OrderID (FK)
ProductID (FK)
PRODUCT
ProductID (PK)
PRODUCT is read only and just used to store the list of products.
When I try to create a new record in ORDERPRODUCTS I get:
An unhandled exception of type 'System.Data.InvalidConstraintException'
occurred in system.data.dll
Additional information: ForeignKeyConstraint OrdersOrderProducts
requires the child key values (0) to exist in the parent table.
First I create a new ORDER (works fine), then I use some code to return
the value of the OrderID just created. I use this to create a new
ORDERPRODUCTS with the ProductID (which is already known and exists the
parent table). But I get the above error. Why? I'm sure I'm doing it in
the right order: ORDER>ORDERPRODUCTS. All the parents get created/exist
first before the child record is created. The child key values DO exist
in the parent tables.
Please help, this is driving me mad.
I use the following code:
Dim drOrders As DataRow = dsDataset.Orders.NewRow
Dim drOrderProducts As DataRow = dsDataset.OrderProducts.NewRow
drOrders.Item("ShippingName") = txtDeliveryName1.Text & " " &
txtDeliveryName3.Text
...
drOrders.Item("TotalPrice") = 100
dsDataset.Orders.Rows.Add(drOrders)
daOrders.Update(dsDataset, "Orders")
AddHandler daOrders.RowUpdated, AddressOf HandleRowUpdated
daOrders.Dispose()
conADOConnection.Close()
conADOConnection.Open()
Dim counter As Integer = 0
' For counter = 0 To dtShoppingBasket.Rows.Count - 1
drOrderProducts.Item("OrderID") = intTempOrderID
drOrderProducts.Item("ProductCode") =
dtShoppingBasket.Rows(counter).Item("ProductCode")
drOrderProducts.Item("AmountRequired") =
dtShoppingBasket.Rows(counter).Item("ProductAmount")
drOrderProducts.Item("PackingOptions") =
dtShoppingBasket.Rows(counter).Item("PackingOptions")
dsDataset.OrderProducts.Rows.Add(drOrderProducts)
Debug.WriteLine(drOrderProducts.Item("OrderID"))
Debug.WriteLine(intTempOrderID)
'Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("ProductCode"))
Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("ProductAmount"))
Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("PackingOptions"))
daOrderProducts.Update(dsDataset, "OrderProducts")
End Sub
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As
OleDb.OleDbRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso e.StatementType =
StatementType.Insert Then
e.Row("OrderID") =
Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString())
e.Row.AcceptChanges()
intTempOrderID = CInt(e.ToString) 'global variable
End If
End Sub
modelled only a few of these in the XML Schema (enough to get my
program working) and assigned the relevant DataRelations.
Anyway I'm having problems with a three table
Order-OrderProducst-Product relationship which stores the temporary
order information in a Shopping Basket datatable.
ORDER
OrderID (PK) (Autonum)
ORDERPRODUCTS
OrderProductsID (PK) (Autonum)
OrderID (FK)
ProductID (FK)
PRODUCT
ProductID (PK)
PRODUCT is read only and just used to store the list of products.
When I try to create a new record in ORDERPRODUCTS I get:
An unhandled exception of type 'System.Data.InvalidConstraintException'
occurred in system.data.dll
Additional information: ForeignKeyConstraint OrdersOrderProducts
requires the child key values (0) to exist in the parent table.
First I create a new ORDER (works fine), then I use some code to return
the value of the OrderID just created. I use this to create a new
ORDERPRODUCTS with the ProductID (which is already known and exists the
parent table). But I get the above error. Why? I'm sure I'm doing it in
the right order: ORDER>ORDERPRODUCTS. All the parents get created/exist
first before the child record is created. The child key values DO exist
in the parent tables.
Please help, this is driving me mad.
I use the following code:
Dim drOrders As DataRow = dsDataset.Orders.NewRow
Dim drOrderProducts As DataRow = dsDataset.OrderProducts.NewRow
drOrders.Item("ShippingName") = txtDeliveryName1.Text & " " &
txtDeliveryName3.Text
...
drOrders.Item("TotalPrice") = 100
dsDataset.Orders.Rows.Add(drOrders)
daOrders.Update(dsDataset, "Orders")
AddHandler daOrders.RowUpdated, AddressOf HandleRowUpdated
daOrders.Dispose()
conADOConnection.Close()
conADOConnection.Open()
Dim counter As Integer = 0
' For counter = 0 To dtShoppingBasket.Rows.Count - 1
drOrderProducts.Item("OrderID") = intTempOrderID
drOrderProducts.Item("ProductCode") =
dtShoppingBasket.Rows(counter).Item("ProductCode")
drOrderProducts.Item("AmountRequired") =
dtShoppingBasket.Rows(counter).Item("ProductAmount")
drOrderProducts.Item("PackingOptions") =
dtShoppingBasket.Rows(counter).Item("PackingOptions")
dsDataset.OrderProducts.Rows.Add(drOrderProducts)
Debug.WriteLine(drOrderProducts.Item("OrderID"))
Debug.WriteLine(intTempOrderID)
'Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("ProductCode"))
Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("ProductAmount"))
Debug.WriteLine(dtShoppingBasket.Rows(counter).Item("PackingOptions"))
daOrderProducts.Update(dsDataset, "OrderProducts")
End Sub
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As
OleDb.OleDbRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso e.StatementType =
StatementType.Insert Then
e.Row("OrderID") =
Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString())
e.Row.AcceptChanges()
intTempOrderID = CInt(e.ToString) 'global variable
End If
End Sub