Question

I am loading data from access database from a datatable name car table. This table has the carId as an auto increment numberenter image description here

loading data this way:

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

        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"))
        DailyChargeTextBox.DataBindings.Add(New Binding("text", bsCar, "dailyCharge"))
        WeeklyChargeTextBox.DataBindings.Add(New Binding("text", bsCar, "weeklyCharge"))
        MonthlyChargeTextBox.DataBindings.Add(New Binding("text", bsCar, "monthlyCharge"))
        YearlyChargeTextBox.DataBindings.Add(New Binding("text", bsCar, "yearlyCharge"))
        TransmissionTextBox.DataBindings.Add(New Binding("text", bsCar, "transmission"))
        ColorTextBox.DataBindings.Add(New Binding("text", bsCar, "color"))
        RemarkTextBox.DataBindings.Add(New Binding("text", bsCar, "remark"))


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

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

    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
    Try
        Me.Validate()
        bsCar.EndEdit()
        daCar.Update(dsDataSet.Tables("car"))
        MsgBox("Car saved successfully")
    Catch ex As Exception
        MsgBox(Err.Description)
    End Try
End Sub

Now When I add a new car and save it, everything works perfect. But I need the carId of the new car added to be displayed immediately after adding. How can I do that without querying the database? Thank you

Was it helpful?

Solution

It seems that there is way to get the AutoIncrement value from the database during the excution of Update method, but there is no way to avoid another query to the database.
You can use the RowUpdated event raised by the OleDbDataAdapter after a row has been updated (or inserted).

We need to use the "SELECT @@IDENTITY" command supported by Access, but this cannot be effective if we don't work on the same connection used by the Update thus the only option is to use the RowUpdated event where the connection is still open

' Need to have a OleDbCommandBuilder to build the InsertCommand for the OleDbDataAdapter
Sub loadCars()
    ....
    daCar = New OleDbDataAdapter(sql, oledbConn)
    Dim builder = new OleDbCommandBuilder(daCar)
    daCar.InsertCommand = builder.GetInsertCommand
    daCar.Fill(dsDataSet, "car")
End Sub


' In the update code
AddHandler daCar.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf NewRowInserted)
daCar.Update(dsDataSet.Tables("car"))

' The event handler is called for any row inserted, updated or deleted
Sub NewRowInserted(sender as Object, 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)
       Dim newID = DirectCast(cmd.ExecuteScalar(), Integer)
       Console.WriteLine(newID)
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top