473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Concurrency violation SOLVED

I solved the problem. Here is what you have to do if your table has an
autonumber field (like mine):

1. Make a new row with the NewRow Method:
DataRow dr = dataTable.NewRow();
2. Add the new row to the Rows collection of the dataTable:
dataTable.Rows.Add(dr);
3. Open the SqlCeConnection:
conn.Open();
4. Use the DataAdapter's Update method
dataAdapter.Update(dataTable);
5. Select the identity (autonumber generated in the database) with the
following SqlCeCommand:
SqlCeCommand cmd = new SqlCeCommand(@"SELECT @@IDENTITY FROM " +
dataTable.TableName, conn);
6. Use the ExecuteScalar method of the SqlCeCommand like this:
object ident = cmd.ExecuteScalar();
7. Close the connection (optional):
conn.Close();
8. If your autonumber column is the first column in the table (like in most
cases) then that column is read only. You must set the ReadOnly property to
false like this:
dataTable.COlumns[0].ReadOnly = false;
and then do the following:
dr[0] = Int32.Parse(ident.ToString());
dr.AcceptChanges();
9. Now set the ReadOnly property back to true like this:
dataTable.Columns[0].ReadOnly = true;

That's it!!!
If you wonder why you have to do all that to insert a new row into the
database, this is why (that's what I think):

I found out that the database has it's own counter for the autonumber field,
and the dataTable has it's own counter aswell (it depends how you set it).
Imagine that you have one database with one table named TEMP, and 2 users.
Say that you have 1 record in the TEMP table with the autonumber field set
to 100. If the first user enters a new record into TEMP, that record will
have the autonumber field set to 101 in the database, aswell as the
dataTable. Now if the second user enters a new record into TEMP, that record
will have the autonumber filed set to 102 in the database, but 101 in the
dataTable. I think you see the picture.

My problem was a bit different to this one. I had only one user (me). When I
enered the first record into TEMP the autonumber field was set to 101 in
both the database, and the dataTable. But when I enered the second record
into TEMP the autonumber filed was set to 102 in the database, and for some
reason to 103 in the dataTable. When I tryed to update the record with the
autonumber 103, I got a Concurrency violation because there was no record
103 in the database. Weird!!!
Nov 17 '05 #1
0 1280

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Billy Jacobs | last post by:
I have a client who is getting a concurrency violation error whenever she tries to update a record. I can log in to the site as her simultaneously and update the record with no problem. I can...
3
by: Suzanne | last post by:
Hi All I'm having problems getting my data adapter to throw a concurrency exception with an INSERT command. I want to throw a concurrency exception if an attempt is made to enter a row into...
25
by: nick | last post by:
I'm having trouble updating from a datagrid. It's says "Concurrency violation: the UpdateCommand affected 0 records", though I can't see how it's related to "concurrency". I can insert and delete...
2
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the...
4
by: Steven Nagy | last post by:
Hi Have a problem that consistantly occurs in my applications where using a DataAdapter (OLEDB) and a dataset. Using a simple process of adding a row to the dataset table and then calling the...
2
by: Vladimir Oľura | last post by:
I am building a pocket pc application that requires a datagrid. I am inserting a new row this way: private void mInsert_Click(object sender, System.EventArgs e) { try { DataRow dr =...
4
by: Jerry | last post by:
Hi, I have an app which retrieves data from a sql server table and displays it on a datagrid. If 2 sessions of this app are running and 2 users try to update the same record at about the same...
2
by: Agnes | last post by:
For the previous post(2 months ago), I think I had solved the problem however, i got the same error Form A (tableA) (after insert the new record , save it, and then amend the record, )no...
5
by: Vayse | last post by:
In my save code, most of items save fine. But sometimes I get a concurrency violation message. "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." It happens on the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.