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
The code structure seems a little iffy. I think you might find the ADO.Net tutorial in my sig useful. With the code structure and commands in the tutorial, you won't encounter any of the problems that you mentioned, and it will look cleaner.
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Yeah, I looked at that doc before I decided to post the question, but I thought it would be an easy fix. I guess not, so I decided to start fresh using the introduction to ADO.NET as the base and slowly build my program onto it.
So after a few hours I am at the same spot. I still get: "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." if I attempt to delete a newly added record without restarting the app. I also get the same error if I try to update an exsisting record. Adding to and looking through the records works fine. This is day 3 stuck going no where with this grrrr!!! The error happens at da.Update(ds), so I am guessing that the problem is somewhere in the below code.
I'm getting the same problems. The problem is in the way the code is handling concurrency. I can't research it now, but i think if you google concurrency violations in ADO.NET it will pull up some useful stuff. Post if you find a solution!
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
It's the delete command SQL.... it needs a ID in order to delete from the database... since the newly added rows do not have an ID (since it's not saved to the DB yet) so the delete doesn't work....
Supposedly there's a way to compensate, but I don't use DataAdapters much, so I couldn't tell you what the answer is.
I tried remarking out the update commands and just have it add and delete from the dataset and then do an da.update(ds) when the user closes the program. It works great, you can add and delete and update all day, but when you close the program you still get the same error.
From what I have read you shouldn't get a concurrency error unless two users are trying to edit the same row at the same time. All documentation on this error says to catch the error, but that wouldn't help me.
I'm still looking, I will post a solution if I find anything.
You really shouldnt be specifying an ID in code as its going to create issues with multiusers. Set your ID field as an Autoincrement field so the database will increment it automatically upon the addition of a new row.
When deleting you want to "clear the textboxes by either having them populated with the next or previous record in the dataset or by just setting each textbox to an empty string.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I don't know, I have pretty much given up at this point. After I add a new record (without closing the program), I extract the data from the table and the new record is there. So, if the new record is in the database with a correct ID number why do still need to restart the program to delete the new record. Again, I can't modify records old or new.
I tried setting the ID manually as it will always be 1+ the index, but I still get the same error.
Ok I lied, I never give up. I added the following and I can now delete newly added records without restarting the app: da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Ok I seriously don't know. It seems to delete newly added records when it wants to, if not it thows the same error. Modifying an existing record is a definite no go. I've tried to figure it out for the last 10 hours, I'm burnt out on this #$#@###$$%%#!##!$@#$#@%$%@%%@ ( < random cursing). I attached the full code. This is not the full program, it's a skimmed down version. I'm just trying to get one bloody table working right, then modify the full program once the problem is identified. The database is included (no records).
Last edited by Mattastic; Nov 23rd, 2007 at 07:47 PM.
Reason: Spoke to soon, still doesn't work right