I am using an Access db with auto number as the primary key (if that matters). I used this tutorial http://www.homeandlearn.co.uk/NET/nets12p10.html as the bases for my program as I am new to this.
To make this simple; The program works fine, but you must close the program and reopen it before deleting a newly added record or you will get this error: Concurrency violation: the DeleteCommand affected 0 of the expected 1 records. You can delete records fine after restarting the program.
It has to be a simple newbie mistake, but I have been up and down the code comparing it to the tutorial and I can't find anything wrong. Below are the sections of code that are relevent. Any help would be greatly appreciated.
Code:Imports System.Data Imports System.Data.OleDb Public Partial Class MainForm Public dsPublic As New DataSet Public daEmployee As New OleDb.OleDbDataAdapter Sub BtnF1SchedClick(ByVal sender As Object, ByVal e As EventArgs) fillda() F4Sched.F4Initialize() End Sub ... End Class Public Module Functions1 Public con As New OleDb.OleDbConnection Public Sub Fillda() Dim sql As String Dim mydir As Reflection.Assembly = _ System.Reflection.Assembly.GetExecutingAssembly() defaultdir = mydir.Location.Substring(0, InStrRev _ (mydir.Location, "\")) Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB._ 4.0;Data Source=" & MainForm.Source Con.Open() Sql = "Select * from Schedule Order By ThisDay Desc" MainForm.daSched = New OleDb.OleDbDataAdapter(sql, con) MainForm.daSched.Fill(MainForm.dsPublic, "Schedule") End Sub End Module Public Partial Class F5Day Sub BtnF5AddClick(ByVal sender As Object, ByVal e As EventArgs) '***********Add Record********************** Dim dsNewRow As DataRow Dim cb As New OleDb.OleDbCommandBuilder_ (MainForm.daSched) dsNewRow = MainForm.dsPublic.Tables_ ("Schedule").NewRow() dsNewRow.Item("WhoIs") = lstF5Emp.SelectedIndex dsNewRow.Item("StartShift") = txtF5Start.Text dsNewRow.Item("EndShift") = txtF5End.Text dsNewRow.Item("OnCall") = ckbxOnCall.Checked dsNewRow.Item("ThisDay") = ThisDay MainForm.dsPublic.Tables("Schedule").Rows.Add_ (dsNewRow) MainForm.daSched.Update(MainForm.dsPublic, "Schedule") FillSchedule() 'Add new record to the list End Sub Sub BtnF5RemoveClick(ByVal sender As Object, ByVal e As EventArgs) '***********Remove Record******************* Dim cb As New OleDb.OleDbCommandBuilder_ (MainForm.daSched) MainForm.dsPublic.Tables("Schedule").Rows(StartRec +_ lstF5Sched.SelectedIndex).Delete() MainForm.daSched.Update(MainForm.dsPublic, "Schedule") lstF5Sched.Items.Clear() 'Clear the list FillSchedule() 'Refresh data End Sub




Reply With Quote