Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TableAdapter Update Error with DBConcurrency (in VB.NET)

Posted on 2014-04-20
5
Medium Priority
?
937 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 2000 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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