Vb.Net为两个适配器添加rowUpdate事件

时间:2014-02-16 08:06:24

标签: vb.net events save tableadapter

查看关系enter image description here

我正在尝试实施汽车租赁系统。这是关系的一部分,而不是完整的关系。这种关系说汽车可以多次投保,但也有现行的保险证。表carInsurance和carInsurance_1是相同的表,但ms访问显示图中的关系。 carID和insID是自动增量字段。 我正在从数据库加载数据并使用两个绑定源:bsCar和bsInsurance来浏览数据。当我按下保存时,我保存新车并从数据库中获取此车的新carID。然后保存carInsurance并再次尝试获得此保险的最后一个insID。然后通过将car.insID设置为carInsurance表中保存的最后一个insID来再次更新汽车。

当我添加新车并尝试保存时,会出现问题。 NewRowInserted事件将添加到bsCar而不是bsInsurance。每次我调试程序时,我总是得到新的carID,而不是新的insID。此外,当使用F11进行dibuging时,

不会触发事件
daInsurance.Update(dsDataSet.Tables("insurance"))

以下是完整代码:

Public Class frmCar


    Dim oledbConn As OleDbConnection
    Dim dsDataSet As DataSet

    Dim daCar As OleDbDataAdapter
    Dim bsCar As BindingSource
    Dim cbCar As OleDbCommandBuilder

    Dim daInsurance As OleDbDataAdapter
    Dim bsInsurance As BindingSource
    Dim cbInsurance As OleDbCommandBuilder

    Private Sub frmCar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dsDataSet = New DataSet
            loadCars()

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

            dgvInsurance.DataSource = bsInsurance ' dsDataSet.Tables("insurance")
            bsInsurance.Filter = "carId=" & CarIDTextBox.Text
            bsInsurance.Position = bsInsurance.Find("insId", InsIDTextBox.Text)

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

    Sub loadCars()
        Dim sql As String

        Try
            oledbConn = New OleDbConnection(oledbConnString)
            oledbConn.Open()

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

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

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Try
            bsCar.AddNew()
            bsInsurance.AddNew()
            bsInsurance.Filter = "carId=0"

            carMode = "newCar"
            insMode = "newInsurance"

            FullInsuranceCheckBox.Checked = False
            ThirdPartyCheckBox.Checked = False
            InsideOmanCheckBox.Checked = False
            InsideUAECheckBox.Checked = False

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

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

        Dim newCarID As Integer
        Dim newInsID As Integer

        If carMode = "newCar" Then
            Try

                AddHandler daCar.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf NewRowInserted)
                AddHandler daInsurance.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf NewRowInserted)

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

                newCarID = newID

                CarIDTextBox.Text = newCarID
                CarIDTextBox1.Text = newCarID    

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

                newInsID = newID
                InsIDTextBox.Text = newInsID
                InsIDTextBox1.Text = newInsID

                RemoveHandler daCar.RowUpdated, AddressOf NewRowInserted
                RemoveHandler daInsurance.RowUpdated, AddressOf NewRowInserted

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

                MsgBox("Car saved successfully")

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

        End If

    End Sub

    Sub NewRowInserted(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)
            newID = DirectCast(cmd.ExecuteScalar(), Integer)
        End If
    End Sub

End Class

0 个答案:

没有答案