Solved

TableAdapter Update Error with DBConcurrency (in VB.NET)

Posted on 2014-04-20
5
899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query 5 44
Checking for column changes SQL 2014 4 25
MS Access Pasthru Query to Execute SQL Stored procedure 8 46
user database (login sql or login windows) 3 32
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

697 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