Question Databinding textbox does not refresh on the form after update occured

Mimosa777

Active member
Joined
Aug 28, 2014
Messages
28
Programming Experience
1-3
Hi everyone,

I've been trying to fix that issue for some times now and i need your help please.
Basically, i have 2 forms (form1 and form2). I have a set of textboxes on form1 that are bind on a dataset via a bindingsource. when form1 is loaded, all textboxes are filled in correctly. Now i want to update one textbox on form1 via form2. Basically form2 is just a simple form with one textbox and a button. The user is asked to put the value he wants in that textbox on form2 and when he clicked on the button, it saves the new value in the database - then form2 get closed and we are back into form1 (main form).
here is my code to do that :
VB.NET:
Private Sub btnUpdateKM_Click(sender As Object, e As EventArgs) Handles btnUpdateKM.Click
        'open DB connection
        Conn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|dbServauto.mdb")
        Conn.Open()
        'start Transaction
        Trans = Conn.BeginTransaction
        Dim KM = tbKM.Text
        Dim IDVehicule = tbVehiculeID.Text

        'define a command for the sql statement
        Cmd = New OleDb.OleDbCommand("update tblCars set KM= " & KM & " where ID=:0", Conn, Trans)
        Cmd.Parameters.AddWithValue(":0", IDVehicule)
        'execute the command
        Cmd.ExecuteNonQuery()
        'save the transaction
        Trans.Commit()
        'close the connection
        Conn.Close()
        'Form1.KMTextBox.Text = tbKM.Text
        tbKM.Text = ""
        Me.Close()
    End Sub
and here is how my textbox in binded on form1 :
VB.NET:
KMTextBox.DataBindings.Clear()
KMTextBox.DataBindings.Add(New Binding("text", bindingcar, "KM"))

Everything works fine with the update. All I want to see now is that new value showing in form1 without having to close my app and open it again. I've tried the bindingsource.resetbinding method but did not work.

The only way I found which I believe is not the correct way is to directly change the form1.textbox.text = form2.textbox.text which shows the new value as I wanted but my bindingsource is still having in memory the old value.

Hope I was clear enough and thanks in advance for your help. :encouragement:
 
I just don't know then how to test the value return by the update.
Dim value = daCars.Update(dscar, "Cars")
 
Finally, it is working like a charm. Thanks to you JMC. I have rework my coding and realized i had some issues with the way my bindingsources were filled in from the datasets. Anyway, here is my final code and i dont even use ResetCurrentItem() anymore. I know you were questioning why i needed to use ResetCurrentItem from the begining. Everything works great now. Thanks again for your help regarding my connections and the coding tips as well.
VB.NET:
Private Sub btnUpdateKM_AddF_Click(sender As Object, e As EventArgs) Handles btnUpdateKM_AddF.Click
        Using dialogue As New Form2
            Dim currentrow = DirectCast(TblCarsBindingSource.Current, DataRowView)
            dialogue.tbKM.Text = CStr(currentrow("KM"))
            If dialogue.ShowDialog() = DialogResult.OK Then
                currentrow("KM") = dialogue.tbKM.Text
                Me.Validate()
                Me.TblCarsBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DbServautoDataSet)
            End If
        End Using
    End Sub
To JohnH, thank you too for the tip. you all rocks :beer::beer:
 
Back
Top