Vb.Net更新表单,包含两个bindingSources的详细信息

时间:2014-02-19 19:12:39

标签: vb.net forms save sequence bindingsource

我有一张表格可以从两张表中加载汽车和保险详情。看一下关系:enter image description here

CarId和insID是自动增量 字段

以下是表单的外观: enter image description here

轻松说每辆车每年都有保险,而且还有现行的保险身份证。

以下是我从数据库加载数据的方式:

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")

绑定如下:

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)

以这种方式添加新车:

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

现在问题在于保存按钮: 我的想法是保存新车并立即获得新的carID并将carID文本框设置为此号码以获取汽车和保险详情。然后保存保险表并从数据库中获取新的insId,并将insId文本框设置为表单中汽车和保险部分的新insId。最后用新的insId再次保存汽车表,这使整个关系充满了。 这是保存代码:

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

上述代码无法正常运行。在我打印的每条消息看到表单的值后,我发现一些字段值消失了,没有设置为我想要的! 解决我问题的最佳代码序列是什么?

1 个答案:

答案 0 :(得分:0)

这就是因为添加新按钮中的行bsInsurance.Filter = "carId=0"。因此,当我添加新记录时,一切正常,数据保存到数据库但由于过滤器未设置为新的insId而消失。因此,在保存按钮中添加bsInsurance.Filter = "carId=" & newInsId