EDN Admin
Well-known member
I am using Visual Studio 2008<br/>
<br/>
I am having problems inserting new child rows linked to a new parent row.<br/>
<br/>
In my sample app, I am using the Nwind Access database. <br/>
<br/>
I have created a nwindDataSet that has two tables dataset Orders and Order Details. The dataset has a Relation “OrdersOrder Details” between the two tables. The relation is a One to Many on the OrderId fields, with Orders being the Parent Table
and Order Details being the child table. The relation is “Both Relation and Foreign Key Constraint”. The Update rule is Cascade, the Delete rule is Cascade and the Accept/Reject rule is None.<br/>
<br/>
On my Form, I have:<br/>
<br/>
<span class="x_Apple-tab-span" style="white-spacere NwindDataSet1 – a .nwindDataSet<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSetName = nwindDataSet<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersBindingSource – a System.Windows.Forms.BindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = NwindDataSet1<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataMember = Orders<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersTableAdapter – a .nwindDataSetTableAdaptersOrdersTableAdapter<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersOrderDetailsBindingSource – a System.Windows.Forms.BindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataMember = OrdersOrder Details<br/>
<span class="x_Apple-tab-span" style="white-spacere Order_DetailsTableAdapter – a .nwindDataSetTableAdaptersOrders_DetailTableAdapter<br/>
<br/>
<span class="x_Apple-tab-span" style="white-spacere DataGridView1 – a System.Windows.Forms.DataGridView<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .Dock = Top (at top of form)<br/>
<span class="x_Apple-tab-span" style="white-spacere DataGridView2 – a System.Windows.Forms.DataGridView<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersOrderDetailsBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .Dock = Top (under DataGridView1)<br/>
<span class="x_Apple-tab-span" style="white-spacere Button1 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “Ale Combo”<br/>
<span class="x_Apple-tab-span" style="white-spacere Button2 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “New Order”<br/>
<span class="x_Apple-tab-span" style="white-spacere Button3 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “New Order Ale Combo”<br/>
<br/>
When I click Button1, I have multiple rows added to the Order Details table for the current Order row. This works fine. I see the new rows in DataGridView2.<br/>
<br/>
When I click Button2, I have a new Order row added. This works fine. I see the new Orders row in <br/>
DataGridView1, and no Order Detail rows in DataGridView2. I am able to manually add Order Detail rows to DataGridView2 <br/>
<br/>
When I click Button3, I want to add a new Order row and add multiple rows to the Order Details table for the new Order row. I am able to add the new Order row, but not able to add any rows to the Order Details table. The exception I get is: <br/>
<span class="x_Apple-tab-span" style="white-spacere System.Data.InvalidConstraintException was unhandled Message="ForeignKeyConstraint OrdersOrder Details requires the child key values (-1) to exist in the parent table."<br/>
<br/>
Why can I add rows manually (after clicking Button2), but not via code? <br/>
<br/>
Code in Form1.:
<pre class="prettyprint lang-vb Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TODO: This line of code loads data into the NwindDataSet1.Orders table. You can move, or remove it, as needed.
Me.OrdersTableAdapter.Fill(Me.NwindDataSet1.Orders)
TODO: This line of code loads data into the NwindDataSet1.Order_Details table. You can move, or remove it, as needed.
Me.Order_DetailsTableAdapter.Fill(Me.NwindDataSet1.Order_Details)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddAleCombo()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OrdersBindingSource.AddNew()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
OrdersBindingSource.AddNew()
AddAleCombo()
End Sub
Private Sub AddAleCombo()
Const idSasquatchAle As Integer = 34
Const idSteeleyeStout As Integer = 35
Const idLaughingLumberjackLager As Integer = 67
OrdersBindingSource.Current returns a DataRowView, so use OrdersBindingSource.Current.Row
Dim OrderRow As nwindDataSet.OrdersRow = CType(OrdersBindingSource.Current.Row, nwindDataSet.OrdersRow)
Dim DetailTable As nwindDataSet.Order_DetailsDataTable = NwindDataSet1.Tables("Order Details")
get new detail row
Dim DetailRow As nwindDataSet.Order_DetailsRow = DetailTable.NewOrder_DetailsRow
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idSteeleyeStout populate detail row
DetailRow.UnitPrice = 12D
DetailRow.Quantity = 1
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
DetailRow = DetailTable.NewOrder_DetailsRow get another new detail row
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idSasquatchAle populate detail row
DetailRow.UnitPrice = 11D
DetailRow.Quantity = 2
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
DetailRow = DetailTable.NewOrder_DetailsRow get another new detail row
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idLaughingLumberjackLager populate detail row
DetailRow.UnitPrice = 10D
DetailRow.Quantity = 3
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
End Sub
End Class[/code]
<br/>
<br/>
View the full article
<br/>
I am having problems inserting new child rows linked to a new parent row.<br/>
<br/>
In my sample app, I am using the Nwind Access database. <br/>
<br/>
I have created a nwindDataSet that has two tables dataset Orders and Order Details. The dataset has a Relation “OrdersOrder Details” between the two tables. The relation is a One to Many on the OrderId fields, with Orders being the Parent Table
and Order Details being the child table. The relation is “Both Relation and Foreign Key Constraint”. The Update rule is Cascade, the Delete rule is Cascade and the Accept/Reject rule is None.<br/>
<br/>
On my Form, I have:<br/>
<br/>
<span class="x_Apple-tab-span" style="white-spacere NwindDataSet1 – a .nwindDataSet<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSetName = nwindDataSet<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersBindingSource – a System.Windows.Forms.BindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = NwindDataSet1<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataMember = Orders<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersTableAdapter – a .nwindDataSetTableAdaptersOrdersTableAdapter<br/>
<span class="x_Apple-tab-span" style="white-spacere OrdersOrderDetailsBindingSource – a System.Windows.Forms.BindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataMember = OrdersOrder Details<br/>
<span class="x_Apple-tab-span" style="white-spacere Order_DetailsTableAdapter – a .nwindDataSetTableAdaptersOrders_DetailTableAdapter<br/>
<br/>
<span class="x_Apple-tab-span" style="white-spacere DataGridView1 – a System.Windows.Forms.DataGridView<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .Dock = Top (at top of form)<br/>
<span class="x_Apple-tab-span" style="white-spacere DataGridView2 – a System.Windows.Forms.DataGridView<br/>
<span class="x_Apple-tab-span" style="white-spacere .DataSource = OrdersOrderDetailsBindingSource<br/>
<span class="x_Apple-tab-span" style="white-spacere .Dock = Top (under DataGridView1)<br/>
<span class="x_Apple-tab-span" style="white-spacere Button1 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “Ale Combo”<br/>
<span class="x_Apple-tab-span" style="white-spacere Button2 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “New Order”<br/>
<span class="x_Apple-tab-span" style="white-spacere Button3 – a System.Windows.Forms.Button<br/>
<span class="x_Apple-tab-span" style="white-spacere .Text = “New Order Ale Combo”<br/>
<br/>
When I click Button1, I have multiple rows added to the Order Details table for the current Order row. This works fine. I see the new rows in DataGridView2.<br/>
<br/>
When I click Button2, I have a new Order row added. This works fine. I see the new Orders row in <br/>
DataGridView1, and no Order Detail rows in DataGridView2. I am able to manually add Order Detail rows to DataGridView2 <br/>
<br/>
When I click Button3, I want to add a new Order row and add multiple rows to the Order Details table for the new Order row. I am able to add the new Order row, but not able to add any rows to the Order Details table. The exception I get is: <br/>
<span class="x_Apple-tab-span" style="white-spacere System.Data.InvalidConstraintException was unhandled Message="ForeignKeyConstraint OrdersOrder Details requires the child key values (-1) to exist in the parent table."<br/>
<br/>
Why can I add rows manually (after clicking Button2), but not via code? <br/>
<br/>
Code in Form1.:
<pre class="prettyprint lang-vb Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TODO: This line of code loads data into the NwindDataSet1.Orders table. You can move, or remove it, as needed.
Me.OrdersTableAdapter.Fill(Me.NwindDataSet1.Orders)
TODO: This line of code loads data into the NwindDataSet1.Order_Details table. You can move, or remove it, as needed.
Me.Order_DetailsTableAdapter.Fill(Me.NwindDataSet1.Order_Details)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddAleCombo()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OrdersBindingSource.AddNew()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
OrdersBindingSource.AddNew()
AddAleCombo()
End Sub
Private Sub AddAleCombo()
Const idSasquatchAle As Integer = 34
Const idSteeleyeStout As Integer = 35
Const idLaughingLumberjackLager As Integer = 67
OrdersBindingSource.Current returns a DataRowView, so use OrdersBindingSource.Current.Row
Dim OrderRow As nwindDataSet.OrdersRow = CType(OrdersBindingSource.Current.Row, nwindDataSet.OrdersRow)
Dim DetailTable As nwindDataSet.Order_DetailsDataTable = NwindDataSet1.Tables("Order Details")
get new detail row
Dim DetailRow As nwindDataSet.Order_DetailsRow = DetailTable.NewOrder_DetailsRow
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idSteeleyeStout populate detail row
DetailRow.UnitPrice = 12D
DetailRow.Quantity = 1
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
DetailRow = DetailTable.NewOrder_DetailsRow get another new detail row
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idSasquatchAle populate detail row
DetailRow.UnitPrice = 11D
DetailRow.Quantity = 2
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
DetailRow = DetailTable.NewOrder_DetailsRow get another new detail row
DetailRow.SetParentRow(OrderRow) link to current order
DetailRow.ProductID = idLaughingLumberjackLager populate detail row
DetailRow.UnitPrice = 10D
DetailRow.Quantity = 3
DetailRow.Discount = 0.2
DetailTable.Rows.Add(DetailRow) add detail row to table
End Sub
End Class[/code]
<br/>
<br/>
View the full article