bruno_pascal
Member
- Joined
- Oct 13, 2013
- Messages
- 5
- Programming Experience
- Beginner
Hi everybody
I was doing an appointment book in my visual basic 2008 , registering fields like ID, Name, Address, Phone(Tel), Cellular(Cel), Email and general observations(Obs) into mysql database .
The problem is : when I add a contact and right after I try to delete it , it appears an error which is : Cuncurrency Violation . Delete command affected 0 of 1 expected registers.
Anyone could help me please, giving any suggestions about what should I have do to solve this problem?? If yes,
in which part of the code I have to change??
Thanks in advance to all
The code is :
I was doing an appointment book in my visual basic 2008 , registering fields like ID, Name, Address, Phone(Tel), Cellular(Cel), Email and general observations(Obs) into mysql database .
The problem is : when I add a contact and right after I try to delete it , it appears an error which is : Cuncurrency Violation . Delete command affected 0 of 1 expected registers.
Anyone could help me please, giving any suggestions about what should I have do to solve this problem?? If yes,
in which part of the code I have to change??
Thanks in advance to all
The code is :
Imports MySql.Data.MySqlClient
Public Class frmCadaster
Inherits System.Windows.Forms.Form
Private oDataAdapter As MySqlDataAdapter
Private oDataset As DataSet
Private iRowPos As Integer
Public marker As Integer
Private Sub frmCadaster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oConnection As MySqlConnection
oConnection= New MySqlConnection
oConnection.ConnectionString = "Server=localhost;Database=cadaster;uid =root;pwd=;"
Me.oDataAdapter = New MySqlDataAdapter("Select * from contacts", oConnection)
Dim oCommBuild As MySqlCommandBuilder = New MySqlCommandBuilder(oDataAdapter)
Me.oDataset = New DataSet
oConnection.Open()
Me.oDataAdapter.Fill(oDataset, "contacts")
oConnection.Close()
Me.iRowPos = 0
Me.marker = 0
Me.LoadData()
Me.Textboxs(True)
End Sub
Private Sub LoadData()
Dim oDataRow As DataRow
oDataRow = Me.oDataset.Tables("contacts").Rows(Me.iRowPos)
Try
Me.txtID.Text = oDataRow("ID").ToString
Me.txtName.Text = oDataRow("Name").ToString
Me.txtAddress.Text = oDataRow("Address").ToString
Me.txtTel.Text = oDataRow("Tel").ToString
Me.txtCel.Text = oDataRow("Cel").ToString
Me.txtEmail.Text = oDataRow("Email").ToString
Me.txtObs.Text = oDataRow("Obs").ToString
Catch ex As Exception
MessageBox.Show("Error ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Me.lblRegister.Text = "Register: " & _
Me.iRowPos + 1 & " of " & _
Me.oDataset.Tables("contacts").Rows.Count
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Dispose()
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If Me.iRowPos = (Me.oDataset.Tables("contacts").Rows.Count - 1) Then
Beep()
Return
Else
Me.iRowPos += 1
Me.LoadData()
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If Me.iRowPos = 0 Then
Beep()
Return
Else
Me.iRowPos -= 1
Me.LoadData()
End If
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
Me.iRowPos = 0
Me.LoadData()
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Me.iRowPos= (Me.oDataset.Tables("contacts").Rows.Count - 1)
Me.LoadData()
End Sub
Private Sub Clear()
txtName.Clear()
txtAddress.Clear()
txtTel.Clear()
txtCel.Clear()
txtEmail.Clear()
txtObs.Clear()
txtName.Focus()
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
Me.marker = 1
Clear()
Me.Textboxs(False)
Me.Navigation(False)
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Me.marker = 2
Me.Textboxs(False)
Me.Navigation(False)
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If marker = 0 Then
Dim answer As DialogResult
answer = MessageBox.Show("Are you sure to delete this contact?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If answer = Windows.Forms.DialogResult.Yes Then
Dim oDataRow As DataRow
oDataRow = Me.oDataset.Tables("contacts").Rows(Me.iRowPos)
oDataRow.Delete()
Dim oDeletedTable As DataTable
oDeletedTable = Me.oDataset.Tables("contacts").GetChanges(DataRowState.Deleted)
Me.oDataAdapter.Update(oDeletedTable) '//------error appears at this line
Me.oDataset.Tables("contacts").AcceptChanges()
MessageBox.Show("Contact deleted successfully!", "Exclusion", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.marker = 0
btnPrevious.PerformClick()
Else
Return
End If
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If marker = 1 Then
If txtName.Text <> "" Then
Dim oDataRow As DataRow
oDataRow = Me.oDataset.Tables("contacts").NewRow()
oDataRow("ID") = Me.txtID.Text
oDataRow("Name") = Me.txtName.Text
oDataRow("Address") = Me.txtAdress.Text
oDataRow("Tel") = Me.txtTel.Text.ToString
oDataRow("Cel") = Me.txtCel.Text
oDataRow("Email") = Me.txtEmail.Text
oDataRow("Obs") = Me.txtObs.Text
Me.oDataset.Tables("contacts").Rows.Add(oDataRow)
Me.oDataAdapter.Update(Me.oDataset, "contacts")
MessageBox.Show("Contact saved!", "Added contact", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Textboxs(True)
Me.marker = 0
Me.Navigation(True)
Me.btnLast.PerformClick()
LoadData()
Else
MessageBox.Show("Enter at least a name to save a contact!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
ElseIf marker = 2 Then
Dim oDataRow As DataRow
oDataRow = Me.oDataset.Tables("contacts").Rows(Me.iRowPos)
oDataRow("ID") = Me.txtID.Text
oDataRow("Name") = Me.txtName.Text
oDataRow("Address") = Me.txtAddress.Text
oDataRow("Tel") = Me.txtTel.Text
oDataRow("Cel") = Me.txtCel.Text
oDataRow("Email") = Me.txtEmail.Text
oDataRow("Obs") = Me.txtObs.Text
Me.oDataAdapter.Update(Me.oDataset, "contacts")
MessageBox.Show("Edited contact ", "Edition", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Textboxs(True)
Me.Navigation(True)
Me.marker = 0
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
LoadData()
Textboxs(True)
Me.Navigation(True)
Me.marker = 0
End Sub
Private Sub Textboxs(ByVal value As Boolean)
txtName.ReadOnly = value
txtAddress.ReadOnly = value
txtTel.ReadOnly = value
txtCel.ReadOnly = value
txtEmail.ReadOnly = value
txtObs.ReadOnly = value
End Sub
Private Sub Navigation(ByVal value As Boolean)
btnFirst.Enabled = value
btnPrevious.Enabled = value
btnNext.Enabled = value
btnLast.Enabled = value
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
frmSearch.Show()
End Sub
End Class
Last edited: