Ok, I'm trying to learn the whole access database connection in .Net (note I'm running an older version, so I'm unable to open some of the samples I've seen posted).
Anyhow, my program works for the most part, I'm able to open, scroll through records, delete, update and even add records. However, I run into a problem when I try to delete a record I've added durring the same session (closing then reopening the program, I can delete it fine).
The error appears when I have selected a new record and 'bntDelete_Click'. The error appears on line
DB.da_Test.Update(DB.ds_Test, "Test_Data")
and the message is
UPDATE:
I found the earlier thread http://www.vbdotnetforums.com/showthread.php?t=5137
and did the
and got the same error message - however I added the line 'DB.ds_Test.AcceptChanges()'
after I add a row and it still comes out with the same error.
Any help would be appreciated.
Thanks
Anyhow, my program works for the most part, I'm able to open, scroll through records, delete, update and even add records. However, I run into a problem when I try to delete a record I've added durring the same session (closing then reopening the program, I can delete it fine).
The error appears when I have selected a new record and 'bntDelete_Click'. The error appears on line
DB.da_Test.Update(DB.ds_Test, "Test_Data")
and the message is
VB.NET:
An unhandled exception of type 'System.Data.DbConcurrencyException' occured in system.data.dll
Additional information: Concurrency violoation: The DeleteCommand affected 0 records.
UPDATE:
I found the earlier thread http://www.vbdotnetforums.com/showthread.php?t=5137
and did the
VB.NET:
Try
DB.da_Test.Update(DB.ds_Test, "Test_Data")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
after I add a row and it still comes out with the same error.
Any help would be appreciated.
Thanks
VB.NET:
Public Class DB
Public Shared con As New OleDb.OleDbConnection()
Public Shared ds_Test As New DataSet()
Public Shared da_Test As OleDb.OleDbDataAdapter
End Class
Public Class Form3
Inherits System.Windows.Forms.Form
Dim MaxRows As Integer
Dim inc As Integer
Private Sub bntNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntNext.Click
Dim cb As New OleDb.OleDbCommandBuilder(DB.da_Test) 'To update the database itself, you need this Command Builder
DB.ds_Test.Tables("Test_Data").Rows(inc).Item(0) = txtFirstName.Text
DB.ds_Test.Tables("Test_Data").Rows(inc).Item(1) = txtSurName.Text
DB.da_Test.Update(DB.ds_Test, "Test_Data") 'It appears a Primary key is needed in the Database in order for this command to complete (error message seemed to indicate a different SQL statement may also work)
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("Already at Last Record")
End If
End Sub
Private Sub NavigateRecords()
txtFirstName.Text = DB.ds_Test.Tables("Test_Data").Rows(inc).Item(0)
txtSurName.Text = DB.ds_Test.Tables("Test_Data").Rows(inc).Item(1)
End Sub
Private Sub bntPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("Already at First Record")
End If
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyPath As String
MyPath = CurDir()
DB.con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & MyPath & "/test.mdb"
DB.con.Open()
sql = "SELECT * FROM Test"
DB.da_Test = New OleDb.OleDbDataAdapter(sql, DB.con)
DB.da_Test.Fill(DB.ds_Test, "Test_Data")
DB.con.Close()
txtFirstName.Text = DB.ds_Test.Tables("Test_Data").Rows(0).Item(0)
txtSurName.Text = DB.ds_Test.Tables("Test_Data").Rows(0).Item(1)
inc = 0
MaxRows = DB.ds_Test.Tables("Test_Data").Rows.Count
End Sub
Private Sub bntAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntAdd.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(DB.da_Test)
Dim dsNewRow As DataRow
dsNewRow = DB.ds_Test.Tables("Test_Data").NewRow()
dsNewRow.Item("Test_Name") = TextBox1.Text
dsNewRow.Item("Test_Code") = TextBox2.Text
DB.ds_Test.Tables("Test_Data").Rows.Add(dsNewRow)
DB.da_Test.Update(DB.ds_Test, "Test_Data")
DB.ds_Test.AcceptChanges()
MaxRows = MaxRows + 1
Label1.Text = "record added"
End If
End Sub
Private Sub bntDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntDelete.Click
If MessageBox.Show("Do you really want to Delete this Record? " & txtFirstName.Text, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
Else
Dim cb As New OleDb.OleDbCommandBuilder(DB.da_Test)
DB.ds_Test.Tables("Test_Data").Rows(inc).Delete()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''Getting Error Here
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DB.da_Test.Update(DB.ds_Test, "Test_Data")
DB.ds_Test.AcceptChanges()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
End If
Last edited: