Solved

TableAdapter Update Error with DBConcurrency (in VB.NET)

Posted on 2014-04-20
5
875 Views
Last Modified: 2014-05-07
This may be a basic newbie question.  If I use a tableadapter to fille a datatable, make some changes a datarow and call an UPDATE, make some more changes to the same datarow, then call UPDATE again without reloading the data (through the FILL command), I get a DBConcurrency error.  Is this by design?  Do I need to fill the table again before I call UPDATE a second time?  Here is some example code.  (backend is SQL Server 2008).  Is there an easy way to allow for this so I can minimize roundtrips to the database?


        Using ta As New TestDataSetTableAdapters.ResourceTableAdapter
            ta.FillByResourceID(Me.TestDataSet.Resource, ID)
        End Using
        Dim dr As TestDataSet.ResourceRow
        dr = TestDataSet.Resource(0)
        Using ta As New TestDataSetTableAdapters.ResourceTableAdapter
            dr.Comments = "First"
            ta.Update(Me.TestDataSet.Resource)

            dr.Comments = "Second"
            'This next UPDATE will give the DBConcurrencyException:  Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
            ta.Update(Me.TestDataSet.Resource)
        End Using

Open in new window

0
Comment
Question by:eeyo
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40012684
We tried to reproduce your problem, but it did not happen, and 2nd update worked as expected. See this sample:

https://filedb.experts-exchange.com/incoming/ee-stuff/8347-ee.rar


You will have to adjust connection string. The table we used was created by the following:

create table resource(id int not null primary key,resource varchar(50), comments varchar(50))
insert into resource select 1 ,'none','none'
0
 

Author Comment

by:eeyo
ID: 40016466
I am using a timestamp field as well.  Maybe that's the difference.

It looks like others are having the same issue.  One solution is to just re-fill the datatable again, which can be resource heavy.
Link Here
0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 40016639
You need to re-fill the dataset only if the table on the server has really changed between your updates, such as by another session (another user, or even yourself), or maybe by the trigger. For example, if the table has trigger that puts last-updated timestamp in a column, then your client won't know about that.

This can be addressed by adjusting UPDATE statement of the adapter. The one generated by visual studio will verify all the columns of the row being updated, if they are still the same as when you read the row from the server. If any of them is not the same, that's when you receive the message about concurrency. You can remove those verifications if you are sure it's OK to do.

For example, if the table has column1 primary key, column2 and column3, the standard update statement will be:

update table set column2=@newvalue2 where column1=@primary_key and column2=@oldvalue2 and column3=@oldvalue3

if you are sure that there's in fact no concurrency, and even if column2 or 3 has somehow changed from when you read it, it's ok to override, change to

update table set column2=@newvalue2 where column1=@primarykey
0
 

Author Comment

by:eeyo
ID: 40047896
Unfortunately, I have not been able to resolve the issue with the above suggestions.  I think I will just forgo efficiency and just refill the datatables again.
0
 

Author Closing Comment

by:eeyo
ID: 40047910
Not the direct solution, but there may be some unusual situation with my setup.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 30
Winform Module - What is the ASP.Net equiv 2 20
Loops and updating in SQL Query 9 25
SQL Count issue 24 15
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question