Question

I have a form that loads car and insurance details from two tables. Have a look to the relation:enter image description here

CarId and insID are Auto-increment fields

And the following is how the forms look like: enter image description here

Easily saying that each car is insured every year and also it has a current insurance ID.

And the following is the way I load data from database:

sql = "select * from car order by carId"
daCar = New OleDbDataAdapter(sql, oledbConn)
daCar.Fill(dsDataSet, "car")

sql = "select * from carInsurance order by carId"
daInsurance = New OleDbDataAdapter(sql, oledbConn)
daInsurance.Fill(dsDataSet, "insurance")

And binding is as follows:

bsCar = New BindingSource(dsDataSet, "car")
bsInsurance = New BindingSource(dsDataSet, "insurance")

CarIDTextBox.DataBindings.Add(New Binding("text", bsCar, "carId"))
BrandTextBox.DataBindings.Add(New Binding("text", bsCar, "brand"))
ModelTextBox.DataBindings.Add(New Binding("text", bsCar, "model"))
RegNoTextBox.DataBindings.Add(New Binding("text", bsCar, "regNo"))
InsIDTextBox.DataBindings.Add(New Binding("text", bsCar, "insId"))
.....
InsIDTextBox1.DataBindings.Add(New Binding("text", bsInsurance, "insId"))
CarIDTextBox1.DataBindings.Add(New Binding("text", bsInsurance, "carId"))
InsFromDateDateTimePicker.DataBindings.Add(New Binding("text",bsInsurance, "insFromDate"))
InsToDateDateTimePicker.DataBindings.Add(New Binding("text", bsInsurance, "insToDate"))
...
dgvInsurance.DataSource = bsInsurance ' dsDataSet.Tables("insurance")
bsInsurance.Filter = "carId=" & CarIDTextBox.Text
bsInsurance.Position = bsInsurance.Find("insId", InsIDTextBox.Text)

Adding new car this way:

bsCar.AddNew()
bsInsurance.AddNew()
bsInsurance.Filter = "carId=0"

Now the problem is in the save button: My idea is to save the new car and immediately get the new carID and set the carID textbox to this number for the car and insurance details. Then save the insurance table and get the new insId from the database and set the insId textbox to this new insId for car and insurance parts of the form. And finally save the car table again with the new insId and this makes the whole relation filled up. This is the save code:

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

If carMode = "newCar" Then
      Try

          AddHandler daCar.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf getNewCarID)
          AddHandler daInsurance.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf getNewInsID)

          Me.Validate()

          bsCar.EndEdit()
          cbCar = New OleDbCommandBuilder(daCar)
          daCar.Update(dsDataSet.Tables("car"))

          CarIDTextBox.Text = newCarID
          CarIDTextBox1.Text = newCarID

          MsgBox("")

          bsInsurance.EndEdit()
          cbInsurance = New OleDbCommandBuilder(daInsurance)
          daInsurance.Update(dsDataSet.Tables("insurance"))

          InsIDTextBox.Text = newInsID
          InsIDTextBox1.Text = newInsID

          MsgBox("")

          RemoveHandler daCar.RowUpdated, AddressOf getNewCarID
          RemoveHandler daInsurance.RowUpdated, AddressOf getNewInsID

          bsCar.EndEdit()
          daCar.Update(dsDataSet.Tables("car"))

          MsgBox("Car saved successfully")

       Catch ex As Exception
            MsgBox(Err.Description)
       End Try

    End If

End Sub

Sub getNewCarID(ByVal sender As Object, ByVal e As OleDb.OleDbRowUpdatedEventArgs)
        ' Check if we are working for a row with the proper state
        If e.Row.RowState = DataRowState.Added Then
            Dim cmd = New OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
            newCarID = DirectCast(cmd.ExecuteScalar(), Integer)
        End If
    End Sub

    Sub getNewInsID(ByVal sender As Object, ByVal e As OleDb.OleDbRowUpdatedEventArgs)
        ' Check if we are working for a row with the proper state
        If e.Row.RowState = DataRowState.Added Then
            Dim cmd = New OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
            newInsID = DirectCast(cmd.ExecuteScalar(), Integer)
        End If
    End Sub

The above code does not work properly. After every message I print to see the values of the form I find some fields values disappear and not set as I want! What is the best code sequence to solve my problem?

Was it helpful?

Solution

That is all because the line bsInsurance.Filter = "carId=0" in the add new button. So when I add new record everything work fine and data are saved to the database but it disappears because the filter is not set to the new insId. So in save button add bsInsurance.Filter = "carId=" & newInsId

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top