How to refresh datatable from datasource

  • Thread starter Thread starter alfred.sehmueller
  • Start date Start date
A

alfred.sehmueller

Hello,

in a network-winforms-application based on a access database I've got
the following problem:

user A starts software
user B starts software

user A deletes row in db
user B changes data

application of user B makes an update dataadapter.update(datatable).
Now I get an concurrency violation.

Now I try to handle this error - what I want to do is simply refresh
user B's datatable with datadapter.fill(datatable). But nothing happens
- the datatable is not updated. I already tried .clear before update -
without success.

Can you help me with this issue please?

Thanks Alfred
 
Ah, this is concurrency 101. It's an age old question that has been asked (and answered) since the first time two people tried to sit on the same rock in the garden of Eden. We've discussed this here on this list many times as well.

When you use a disconnected approach to data access (as ADO.NET encourages you to do) you have to understand that the data you've fetched is simply a copy of the data stored in the database. When you do an update (as when you execute the Update method) the data in the database is changed--unless you add rules that say "Ok, if someone else changed the data since I got my copy, don't post my change". This is the default behavior for ADO.NET (as dictated by the CommandBuilder). Yes, there are other rules and ways to determine if a row has changed since you last fetched it but let's just keep it simple for now. So, if someone has changed the row, your Update did not go through. If you requery (run the SELECT again) you'll see the changes made by the other user(s). But there is nothing to stop yet another user from making more changes before you make your change. This is typical of a heavily used database--add more users and you get more collisions.

In my new book, I discuss how to design systems and databases where the users don't share the same rows. I design my data intersections with traffic lights and bridges instead of positioning ambulances nearby to pick up the dead and injured. While I don't know how your database is designed or the problem you're trying to solve with it, there are generally accepted practices we've come up with over the years to deal with concurrency issues.

1.. If you have to design a system where more than one user has access to the rows, you have to be prepared for collisions. In ADO.NET these are manifested as the Update method being told that the "rows affected" value was not 1.
2.. If your update can fail because you permit multiple access to the rows you must decide (in your design) what to do about it. You can
1.. Force through your update. In this approach you might just remove all WHERE clause tests to see if the row has changed. This is called "Last Update wins".
2.. You can fetch the changed row and let the user decide what to do. I don't like this approach as the data can change again a dozen times while Betty or Bob figures out what to do.
3.. You can make a logical decision about what to do based on business rules. For example, accounts from Texas have a lower priority than those from California but no changes are permitted to accounts from Cleveland.
4.. There are other variations on this list... but you get the idea.
3.. If possible revisit 1. See if your design can be modified to prevent collisions instead of cleaning up after them.
hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
I don't have a lot of experience with ADO.NET, but what I do know is
that everything is designed around disconnected recordsets as being the
norm, whereas regular ADO kept its connections always open to the
database.


Is your connection to the database on the dataAdapter still open? Do
you need to re-establish the connection to re-fill the dataset or
table?


I'm experiencing the reverse problem in that I have a table with new
records that won't update the physical database - nothing happens.
I've traced this to the rowstate property on each of the records being
set to DETACHED. Perhaps your table contains records with a rowstate
that prevents the table from being refreshed, such as "ADDED" or
"MODIFIED" that indicates pending updates? I know that there is a
cancel method on the row object - perhaps you need to call that first
to cancel the pending changes before you can do a refresh?
 
Hello William,

When you use a disconnected approach to data access (as ADO.NET encourages you to do) you have to understand that the data you've fetched is simply a copy of the data stored in the database. When you do an update (as when you execute the Update method) the data in the database is changed--unless you add rules that say "Ok, if someone else changed the data since I got my copy, don't post my change"

Yes I know - in my special case the conflict resolution is: "Someone
deleted the row I did update - so discard my changes". I wan't to
complete reload the datatable-data from the database. My problem is not
the error-message but I'm not able to refresh the data again...

Bye Alfred
 
Did you check how many rows are in your datatable after you call Clear? How
many are there after?

Maybe you need to show your refresh code
 
Hello William,



Yes I know - in my special case the conflict resolution is: "Someone
deleted the row I did update - so discard my changes". I wan't to
complete reload the datatable-data from the database. My problem is not
the error-message but I'm not able to refresh the data again...

Bye Alfred

To see the current state of the DataSet you have to set the DataSource on the
grid to null, re-fill the dataset and set the grid's DataSource again. You will
then have the current state of the data on the server. I know, it's a kluge,
but that's the way I have done it in the past. There sure may be a better way.
If someone knows how, I'd like to learn about it too ;o).
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Back
Top