0

I'm trying to make a bulk update in the database using DataAdapter.Update, and the user has the option to cancel the operation.

Question: How to stop the update operation when the user clicks the cancel button?

My Code::

 Log("@@@@@ Saving BMPImages paths to the database @@@@@")
 objCommandBuilder = New SqlCommandBuilder(daImages)
 SavingImageObj.MyProgressBar.Maximum = tblImages.GetChanges.Rows.Count
 AddHandler tblImages.RowChanged, New DataRowChangeEventHandler(AddressOf tblImages_changed)
 daImages.Update(dsMAP, "BMPImages")

And this is the RowChanged Handler::

Private Sub tblImages_changed(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
    'Increment progress bar
    SavingImageObj.ThreadTask()

    If SkipFraming = True Then
        ' Here I should do something to stop the daImages.Update
        ' from continuing to execute
    End If
End Sub

I'm setting a flag [SkipFraming] indicating that i should stop the operation when the cancel button is clicked.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Roshdy
  • 1,703
  • 3
  • 25
  • 34
  • Possible duplicate of [How to cancel a long-running Database operation?](http://stackoverflow.com/questions/889102/how-to-cancel-a-long-running-database-operation) – Lews Therin Oct 11 '15 at 17:12
  • I think that this question has a problem with the background worker (Threading issue) not like my case – Roshdy Oct 12 '15 at 08:46

1 Answers1

0

Ok I found the solution to my problem: I should make a the event handler for the dataAdapter.RowUdpated instead of dataTable.RowChanged in order to be able to stop the update [Which is simply a status update =D (args.Status = UpdateStatus.SkipAllRemainingRows)]

Below is the code sample:

    ' Reset Updating progress counter
    UpdatingProgressCounter = 0

    'Open connection to update database
    objConn.Open()

    'Update database
    Log("@@@@@ Saving BMPImages paths to the database @@@@@")
    objCommandBuilder = New SqlCommandBuilder(daImages)
    SavingImageObj.MyProgressBar.Maximum = tblImages.GetChanges.Rows.Count
    AddHandler daImages.RowUpdated, New SqlRowUpdatedEventHandler(AddressOf daImages_updated)
    Log("========== Total Rows to be updated = [" & tblImages.GetChanges.Rows.Count & "] ==========")
    daImages.Update(dsMAP, "BMPImages")

    'Close database connection
    objConn.Close()


 Private Sub daImages_updated(ByVal sender As Object, ByVal args As System.Data.SqlClient.SqlRowUpdatedEventArgs)
    'Increment progress bar
    SavingImageObj.ThreadTask()

    ' Increment progress counter
    UpdatingProgressCounter += 1

    'Give control on UI
    If UpdatingProgressCounter Mod 10 = 0 Then
        Application.DoEvents()
        SkipFraming = True      'For Testing
    End If

    ' Skip updating remaining rows in case of cancelling the framing
    If SkipFraming = True Then
        Log("@@@@@@@@@@@@@@@@@ Skipping remaining rows @@@@@@@@@@@@@@@@@")
        args.Status = UpdateStatus.SkipAllRemainingRows
    End If
End Sub

I hope it help someone with similar situation.

Roshdy
  • 1,703
  • 3
  • 25
  • 34