1 Attachment(s)
DataRelation - DataBinding - Child table update problem
I have the following code.
vb.net Code:
Const ConnectionString As String = "server=YOURSERVERNAME;database=tempdb;uid=sa;pwd=YOURPASSWORD"
Dim MyDataset As New DataSet
Dim CustomersDA, AddressesDA As SqlDataAdapter
Dim CustomersTable, AddressesTable As DataTable
Dim bsParent, bsChild As BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReBind()
End Sub
Private Sub UpdateDBButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateDBButton.Click
AddressesDA.Update(AddressesTable)
CustomersDA.Update(CustomersTable)
ReBind() '' refill to test whether changes were updated in db.
End Sub
Private Sub ReBind()
MyDataset = New DataSet
CustomersDA = New SqlDataAdapter("select Customers.* from Customers", ConnectionString)
AddressesDA = New SqlDataAdapter("select Addresses.* from Addresses", ConnectionString)
Dim cmdBldr As New SqlCommandBuilder(CustomersDA)
CustomersDA.UpdateCommand = cmdBldr.GetUpdateCommand
cmdBldr = New SqlCommandBuilder(AddressesDA)
AddressesDA.UpdateCommand = cmdBldr.GetUpdateCommand
CustomersDA.Fill(MyDataset, "Customers")
AddressesDA.Fill(MyDataset, "Addresses")
CustomersTable = MyDataset.Tables("Customers")
AddressesTable = MyDataset.Tables("Addresses")
MyDataset.Relations.Add("MyRelation", CustomersTable.Columns("CustomerId"), AddressesTable.Columns("CustomerId"))
bsParent = New BindingSource
bsChild = New BindingSource
bsParent.DataSource = MyDataset
bsParent.DataMember = "Customers"
bsChild.DataSource = bsParent
bsChild.DataMember = "MyRelation"
Me.DataGridView1.DataSource = bsParent
Me.DataGridView2.DataSource = bsChild
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
TextBox1.DataBindings.Add("Text", bsParent, "CustomerName")
TextBox2.DataBindings.Add("Text", bsChild, "City")
End Sub
The problem is that I can see all changes on screen (dataset) whether made from GridViews or the TextBoxes. But the Child Table doesn't update changes to database that were made from the databound textbox (i.e. TextBox2). All other changes are updated.
What am I doing wrong?
Attached is a sample project that depicts the problem and a sql script file to create sample data.
Re: DataRelation - DataBinding - Child table update problem
Nevermind.. I figured out myself.
The DataGridViews automatically call the BindingSource.EndEdit when changing rows. THe textboxes don't call this method. THe changes are updated in Datatables though the RowState remains unchanged. This is mysterious and looks like a bug. Shouldn't the RowState change when data changes?
Anyways, since the problem is known, the solution is easy. Just manually call BindingSource.EndEdit() on respective BindingSources. TextBox Validating method looks like the most appropriate event.
So putting the following code corrects the problem.
vb.net Code:
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Me.bsParent.EndEdit()
Me.bsParent.CurrencyManager.Refresh()
End Sub
Private Sub TextBox2_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
Me.bsChild.EndEdit()
Me.bsChild.CurrencyManager.Refresh()
End Sub