Results 1 to 2 of 2

Thread: DataRelation - DataBinding - Child table update problem

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    DataRelation - DataBinding - Child table update problem

    I have the following code.
    vb.net Code:
    1. Const ConnectionString As String = "server=YOURSERVERNAME;database=tempdb;uid=sa;pwd=YOURPASSWORD"
    2.  
    3.     Dim MyDataset As New DataSet
    4.     Dim CustomersDA, AddressesDA As SqlDataAdapter
    5.     Dim CustomersTable, AddressesTable As DataTable
    6.     Dim bsParent, bsChild As BindingSource
    7.  
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         ReBind()
    10.     End Sub
    11.  
    12.     Private Sub UpdateDBButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateDBButton.Click
    13.         AddressesDA.Update(AddressesTable)
    14.         CustomersDA.Update(CustomersTable)
    15.         ReBind() '' refill to test whether changes were updated in db.
    16.     End Sub
    17.  
    18.     Private Sub ReBind()
    19.         MyDataset = New DataSet
    20.         CustomersDA = New SqlDataAdapter("select Customers.* from Customers", ConnectionString)
    21.         AddressesDA = New SqlDataAdapter("select Addresses.* from Addresses", ConnectionString)
    22.         Dim cmdBldr As New SqlCommandBuilder(CustomersDA)
    23.         CustomersDA.UpdateCommand = cmdBldr.GetUpdateCommand
    24.         cmdBldr = New SqlCommandBuilder(AddressesDA)
    25.         AddressesDA.UpdateCommand = cmdBldr.GetUpdateCommand
    26.  
    27.         CustomersDA.Fill(MyDataset, "Customers")
    28.         AddressesDA.Fill(MyDataset, "Addresses")
    29.  
    30.         CustomersTable = MyDataset.Tables("Customers")
    31.         AddressesTable = MyDataset.Tables("Addresses")
    32.         MyDataset.Relations.Add("MyRelation", CustomersTable.Columns("CustomerId"), AddressesTable.Columns("CustomerId"))
    33.  
    34.         bsParent = New BindingSource
    35.         bsChild = New BindingSource
    36.         bsParent.DataSource = MyDataset
    37.         bsParent.DataMember = "Customers"
    38.         bsChild.DataSource = bsParent
    39.         bsChild.DataMember = "MyRelation"
    40.  
    41.         Me.DataGridView1.DataSource = bsParent
    42.         Me.DataGridView2.DataSource = bsChild
    43.         TextBox1.DataBindings.Clear()
    44.         TextBox2.DataBindings.Clear()
    45.         TextBox1.DataBindings.Add("Text", bsParent, "CustomerName")
    46.         TextBox2.DataBindings.Add("Text", bsChild, "City")
    47.     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.
    Attached Files Attached Files
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.     Me.bsParent.EndEdit()
    3.     Me.bsParent.CurrencyManager.Refresh()
    4. End Sub
    5.  
    6. Private Sub TextBox2_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
    7.     Me.bsChild.EndEdit()
    8.     Me.bsChild.CurrencyManager.Refresh()
    9. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width