Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15244

MYSQL Handle Concurrency Violation (Overwrite)

$
0
0
Hi All,

I have been trying to handle concurrency violations for my app to no avail. The app is going on a network so these may be a regular exception to be dealt with.


Load Datagridview:

Code:

Try
            con = New MySqlConnection(cs)
            If Not con.State = ConnectionState.Open Then
                con.Open()
            End If
            cmd = New MySqlCommand("SELECT * FROM `" & name & "`", con)
            da2 = New MySqlDataAdapter(cmd)
            dt2 = New DataTable
            da2.Fill(dt2)
            cb2 = New MySqlCommandBuilder(da2)
            With dtgRecipes
                  .DataSource = ds2.Tables(0)
            End With
Catch ex As Exception

Here I make changes by different users to cause a concurrency violation deliberately. Then I attempt to update the database as follows.

Code:


  UpdateDatabase()

 Private Sub UpdateDatabase()
        Try
            da2.Update(dt2)
            dt2.AcceptChanges()
        Catch dbcx As DBConcurrencyException
            createTempDataTable()
            Dim response As DialogResult = MessageBox.Show("Overwrite?", "Concurrency Exception", MessageBoxButtons.YesNo)
            ProcessDialogResult(response)
        Catch ex As Exception
            MessageBox.Show("An error was thrown while attempting to update the database.")
        End Try
    End Sub


Private tempDataTable As New DataTable()
 Private Sub createTempDataTable()
        Dim name As String = cmbRecipeNames.Text
        Try
            con = New MySqlConnection(cs)
            If Not con.State = ConnectionState.Open Then
                con.Open()
            End If
            cmd = New MySqlCommand("SELECT * FROM `" & name & "`", con)
            da = New MySqlDataAdapter(cmd)
            da.Fill(tempDataTable)
        Catch ex As Exception
   

 Private Sub ProcessDialogResult(ByVal response As DialogResult)
        Select Case response
            Case DialogResult.Yes
                dt2.Merge(tempDataTable, True)
                da2.Update(dt2)
                dt2.AcceptChanges()
            Case DialogResult.No
                ds2.Merge(tempDataTable)
                MessageBox.Show("Update cancelled")
        End Select
    End Sub


No matter what I have tried, I either get the database and datagridview having same data as the left after the first user change (second user change causing the violation), when I am wanting to overwrite the first users change, or I get another concurrency violation. I have been referring some MS documentation but it is from about 2005.

I am confident there is no other user interactions with database other than mentioned above and I have also tried changing all to dataset from datatable with same result.

Could anybody see where I am going wrong, I am thinking the merge is not working how I have coded it but am stumped after a long day of trying!

Many Thanks.

Viewing all articles
Browse latest Browse all 15244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>