One possible manifestation of the first cause I mentioned above:is that your InsertCommand doesn't retrieve auto-generated IDs from the database. A DataTable will generate temporary IDs that may not be the same as the final IDs generated when the data is saved to the database. If you save new records and then try to edit them and save again, the IDs will not match the records in the database so you will actually be trying to update a different record. That record's data will not match the original data that you have in your app so a concurrency exception is thrown. In that case, you need to retrieve those final IDs and refresh your DataTable when you save.
If you're using a typed DataSet, as the OP is, then this is probably just a matter of configuration. Open your DataSet in the designer, right-click each TableAdapter and select Configure, then click Advanced Options. One of the options should be "Refresh the data table", which is described thusly:If that box is unchecked then the CommandText of the adapter's InsertCommand will look something like this:
sql Code:
INSERT INTO [dbo].[Person] ([GivenName], [FamilyName]) VALUES (@GivenName, @FamilyName)
If the box is checked then the SQL code will look more like this:
sql Code:
INSERT INTO [dbo].[Person] ([GivenName], [FamilyName]) VALUES (@GivenName, @FamilyName);
SELECT PersonId, GivenName, FamilyName FROM Person WHERE (PersonId = SCOPE_IDENTITY())
That SELECT statement gets all the columns for a newly inserted record and sends them back to the DataTable. Most columns will be exactly the same but the PK column may well be different. This will therefore ensure that your DataTable contains the same data as your database, thus no concurrency violations should occur.
Note that, if you're not using a typed DataSet, you can simply add the extra query to the InsertCommand of your data adapter manually. In that case, you can probably omit all but the PersonId column from the column list of your query. Only those columns that might change need to be included.
That works for SQL Server and should for most other proper databases as well. If you're using Access then that option is disabled. That's because the Jet and ACE OLE DB providers don't support multiple SQL statements per command. The same will be true of some other databases too; mostly those that are not server-based. In that case, it's still possible to refresh your DataTable but it takes a bit more work. To learn how, follow the CodeBank link in my signature and check out my thread on Retrieving Auto-generated IDs From Access.