
- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index

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