Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
33
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

Session 26—Handling ADO.NET Errors

269

Writing Errors to the Event Log

Now that you understand how to capture each of the OleDB errors, let’s look at how you might log these errors to the System Event Log as well as display them to the user. Why would you want to write errors to the System Event Log? One reason is that whenever you are dealing with errors in the handling of data, you should provide multiple methods for identifying, troubleshooting, and resolving the issue. One method you can use to help identify certain errors is to write errors to the Event Log. This is especially useful in capturing issues that occur infrequently and may be difficult to track down.

In Listing 26-1 the last statement calls the WriteEvent() function. This function accepts as a parameter an exception and then goes through the process of writing the error summary to the Event Log. In order to use this function, you need to import the

System.Diagnostics namespace using:

<%@ Import Namespace=”System.Diagnostics”%>

Then you can implement the generic event log handler as shown in Listing 26-3 which completes Listing 26-2 discussed earlier in this session:

Listing 26-3 Writing error events to the system event log

Function WriteEvent(ByVal myException as Exception)

Dim sPage as String = Request.Path

Dim Message As String = “Url “ & sPage

Dim sLogName As String = “myLogFile”

Dim oLog as New EventLog

Message = Message & “ Error: “

Message = Message & myexception.message

If (Not EventLog.SourceExists(sLogName)) Then EventLog.CreateEventSource(sLogName,sLogName)

End if

oLog.Source = sLogName

oLog.WriteEntry(Message, EventLogEntryType.Error) End Function

</SCRIPT>

<BODY>

<FORM RUNAT=”server” ID=”Form1”> <H1>

ASP.NET OleDB Exception Handling </H1>

<P>

This example is useful for testing some of the typical errors that may

occur

while connecting to OleDB datasources. Try creating errors in the connection string and sql statements to see how error handling is

reported.

</P>

<P>

<ASP:LABEL RUNAT=”Server” ID=”Label1”>Connection String</ASP:LABEL>

Continued

270

Sunday Morning

Listing 26-3

Continued

<ASP:TEXTBOX ID=”txtConnStr” RUNAT=”server” WRAP=”False” WIDTH=”800”>provider=sqloledb;Data Source=127.0.0.1;Initial Catalog=pubs;User ID=sa;pwd=;</ASP:TEXTBOX>

</P>

<P>

<ASP:LABEL RUNAT=”Server” ID=”Label2”>SQL Statement</ASP:LABEL> <ASP:TEXTBOX ID=”txtSQLStr” RUNAT=”server” WRAP=”False”

WIDTH=”800”>SELECT * FROM

GENERATE ERROR IN SQL</ASP:TEXTBOX> </P>

<P>

<ASP:BUTTON TEXT=”Execute Database Code” ONCLICK=”ExecuteDBCodeBtn_Click” RUNAT=”server” ID=”Button1” />

</P>

<P>

<ASP:DATAGRID ID=”grid1” RUNAT=”server” /> </P>

</FORM>

</BODY>

</HTML>

With this function, you are simply capturing the page that generated the error and, creating a new log file called myLogFile, or opening it if it already exists, then writing the message summary to the log file. That’s it! Now you can open the Event Viewer from your Control Panel Administrative Tools Event Viewer icon and review the log file, as illustrated in Figure 26-2, providing a way for remote administrators and local administrators to troubleshoot issues.

Figure 26-2 Using the Event Viewer to view errors in the Event Log

Session 26—Handling ADO.NET Errors

271

REVIEW

You should now understand how to use the OLEDBException class and the OLEDBErrors collection to retrieve the errors that are produced by the OleDBDataAdapter. As the previous examples have shown, capturing these errors is straightforward — the real difficulty is resolving them!

QUIZ YOURSELF

1.How many OleDB errors can be handled by the OLEDBException class? (See “OLEDBError Object Description.”)

2.How could you implement a global generic OleDB error handler for use across your application? (See “OLEDBError Object Description.”)

3.If the first OleDB error in an OLEDBErrors collection contained the value Invalid column name ‘this’ in its Message property, then what would be the value of the OLEDBException.Message property wrapping this collection? (See “OLEDBError Object Properties.”)

P A R T

V

Sunday Morning

Part Review

The following set of questions is designed to provide you feedback on how well you understood the topics covered during this part of the book. Please refer to Appendix A for the answers to each question.

1.A DataSet object can be created without the use of the (SQL or ADO)

DataAdapter’s Fill method. True/False

2.The DataSet is a container class. True/False

3.Fill in the blank: The ______ property, which is read-only, returns the number of DataTables in a DataTableCollection object.

4.Fill in the blank: The ______ property gets a specified DataTable from a

DataTableCollection object.

5.Fill in the blank: ______ is the process of connecting a server control to a DataSet.

6.You can bind server controls to an array of custom classes. True/False

7.Setting the attribute AutoPostBack=”true” on a server control forces the reposting of the page to the server.

True/False

8.DTC objects are part of the ASP.NET Framework. True/False

274

Part V–Sunday Morning Part Review

9.Fill in the blank: The property ______ is what automatically generates the columns for a DataGrid control.

10.Using the following property/value combination, DataFormatString=”{0:C}” for a field will format the contents as what format type.

a.Decimal

b.Currency

c.Date

d.Fixed

11.To format alternating rows of data in a datagrid, manipulate which property?

a.HeaderStyle

b.FooterStyle

c.ItemStyle

d.AlternatingItemStyle

12.The DataReader control should be closed with the Close method. True/False

13.The U in CRUD stands for what?

a.Understand

b.Update

c.Unique

d.Undo

14.You must code your own modules to provide update, delete, and create functionality.

True/False

15.Fill in the blank: To set the number of records on each page, the ______

property should be set.

16.The RangeValidator Control cannot be used with the DataGrid when in Edit mode.

True/False

Part V–Sunday Morning Part Review

275

17.Data shaping is simply the process of:

a.Reflecting the parent child relationships

b.Manipulating data values

c.Updating a DataSet

d.None of the above

18.A parent-child relationship is a type of hierarchy. True/False

19.The DataSet object can hold no more than 10 data tables. True/False

20.A special provider must be used to shape data with ADO.NET. True/False

21.Try...Catch...Finally is a type of structured error handling. True/False

22.The ADOException class acts as a ______ for the ADOErrors collection.

a.Wrapper

b.Object

c.Holding point

d.None of the above

23.What property provides a short description of the error that was generated?

a.Message

b.NativeError

c.Source

d.None of the above

24.What property retrieves the name of the object that generated the error?

a.Message

b.NativeError

c.Source

d.None of the above

P A R T

VI

Sunday

Afternoon

Session 27

SOAP It Up!

Session 28

Web Services

Session 29

Migrating from ASP to ASP.NET

Session 30

Migrating from ADO to ADO.NET