Now What?

[Previous] [Next]

I hear this question a lot from programmers who call Microsoft Developer Support wanting to know what to do when their optimistic update attempts fail. Just as there's no simple answer to the question, "How do I handle errors in my application?" there's no simple answer here. And yet, it's not overly complicated to handle failed optimistic update attempts. You simply have to figure out what went wrong, why it happened, and what to do about it. That's not so difficult, right?

A lot of programmers I speak with about handling failed optimistic update attempts are surprised when I ask them, "Well, once you understand what went wrong, how do you want your code to react?"

When I explain error handling of optimistic updates to customers, I tend to go back to the same example each time—the same snippet of code we've used throughout most of the book:

 strSQL = "SELECT CustomerID, CompanyName, BalanceDue " & _ "FROM Customers WHERE CustomerID = 7" rsCustomers.Open strSQL, cnDatabase, adOpenStatic, _ adLockBatchOptimistic, adCmdText rsCustomers!BalanceDue = rsCustomers!BalanceDue + 50 rsCustomers.Update rsCustomers.UpdateBatch 

You know from previous chapters that if your code retrieves the customer's balance due and another user enters an update for that same customer before you enter your update, your update attempt fails. There are two common ways to handle this scenario.

The first is to let the user know that the update to the customer's balance due failed and explain why it happened. Provide some way for the user to decide what to do: cancel the order, add the cost of the order to the customer's new balance due, or set the balance due to another amount.

Another possible solution is to programmatically resolve the conflict and inform the user about the conflict and how it was resolved. In this case, you could write code to retrieve the customer's new balance due from the database and simply add the cost of the new order to that balance due. Then you could inform the user that the order was added successfully and the customer's balance due was modified appropriately, but that the new balance due might not be what he or she expected because of a change made by another user.

In either case, you'd have to determine the cause of the update conflict. In this chapter, we'll talk about what features ADO offers to help you determine the cause of the conflict. But first, we'll discuss some general error-handling information.



Programming ADO
Programming MicrosoftВ® ADO.NET 2.0 Core Reference
ISBN: B002ECEFQM
EAN: N/A
Year: 2000
Pages: 131
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net