Handling Database Errors

The .NET Framework includes two classes in the System.Data.SqlClient namespace that are useful in handling SQL Server errors. The SqlException class exposes an Errors property, which is a collection of SqlError class objects. When an error occurs in data access, it will return an instance of the SqlException class. You can then walk through the Errors collection and react to the returned SqlError objects. For example, to just display any data access errors, you could use a Try / Catch block similar to this one:

 Try     ' Perform SQL Server operations here Catch SqlEx As SqlException     ' Handle SQL Server specific errors     Dim err As SqlError     For Each err In SqlEx.Errors         MessageBox.Show("SQL Error " & err.Number & ": " & err.Message)     Next Catch Ex As Exception     ' Handle general errors     MessageBox.Show("Non-SQL Exception " & Ex.Message) End Try 

In addition to operational errors that may be encountered , when multiple users make changes to data within separate DataSet objects and then attempt to update the values in the data source, the question arises as to which one of the conflicting changes will "win." You can control the resolution of this situation through the UpdateCommand property of a SqlDataAdapter object, which may be configured in one of two ways:

  • Optimistic concurrency control The change will be applied only if the source row has not changed since the DataSet was loaded. If someone else has changed a row, your changes will be discarded and you'll get an error instead.

  • The last one wins concurrency control All updates will be applied, regardless of whether changes have already occurred since the row was loaded into the DataSet . At any time, the last saved change will appear in the database, no matter what any other user of the data has done.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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