100% found this document useful (2 votes)
31 views

ASP NET by Example 6th Edition Steven A. Smith 2024 scribd download

ASP

Uploaded by

jurezdragelo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
31 views

ASP NET by Example 6th Edition Steven A. Smith 2024 scribd download

ASP

Uploaded by

jurezdragelo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Visit https://ebookgate.

com to download the full version and


explore more ebooks

ASP NET by Example 6th Edition Steven A. Smith

_____ Click the link below to download _____


https://ebookgate.com/product/asp-net-by-example-6th-
edition-steven-a-smith/

Explore and download more ebooks at ebookgate.com


Here are some recommended products that might interest you.
You can download now and explore!

Pro ASP NET MVC 3 Framework 3rd Edition Steven Sanderson

https://ebookgate.com/product/pro-asp-net-mvc-3-framework-3rd-edition-
steven-sanderson/

ebookgate.com

Microsoft ASP NET 4 Step by Step George Shepherd

https://ebookgate.com/product/microsoft-asp-net-4-step-by-step-george-
shepherd/

ebookgate.com

Mastering ASP Net with VB Net 1st Edition A. Russell Jones

https://ebookgate.com/product/mastering-asp-net-with-vb-net-1st-
edition-a-russell-jones/

ebookgate.com

Pro ASP NET Web API HTTP Web Services in ASP NET 1st
Edition Tugberk Ugurlu

https://ebookgate.com/product/pro-asp-net-web-api-http-web-services-
in-asp-net-1st-edition-tugberk-ugurlu/

ebookgate.com
ASP NET Core 1st Edition Fritz

https://ebookgate.com/product/asp-net-core-1st-edition-fritz/

ebookgate.com

Pro ASP NET SharePoint 2010 Solutions Techniques for


Building SharePoint Functionality into ASP NET
Applications 1st Edition Dave Milner
https://ebookgate.com/product/pro-asp-net-sharepoint-2010-solutions-
techniques-for-building-sharepoint-functionality-into-asp-net-
applications-1st-edition-dave-milner/
ebookgate.com

Flash Game Development by Example Feronato

https://ebookgate.com/product/flash-game-development-by-example-
feronato/

ebookgate.com

Debugging ASP NET 1st ed Edition Jonathan Goodyear

https://ebookgate.com/product/debugging-asp-net-1st-ed-edition-
jonathan-goodyear/

ebookgate.com

Pro ASP NET SharePoint 2010 Solutions Techniques for


Building SharePoint Functionality into ASP NET
Applications 1st Edition Dave Milner (Auth.)
https://ebookgate.com/product/pro-asp-net-sharepoint-2010-solutions-
techniques-for-building-sharepoint-functionality-into-asp-net-
applications-1st-edition-dave-milner-auth/
ebookgate.com
00 71465_FM 11/30/01 10:26 AM Page i

ASP.NET
B Y E X A M P L E

201 West 103rd Street


Steven A. Smith
Indianapolis, Indiana 46290
00 71465_FM 11/30/01 10:26 AM Page ii

Publisher
ASP.NET by Example David Culverwell
Copyright © 2002 by Que Executive Editor
All rights reserved. No part of this book shall be reproduced, Candy Hall
stored in a retrieval system, or transmitted by any means, elec- Acquisitions Editor
tronic, mechanical, photocopying, recording, or otherwise, with- Michelle Newcomb
out written permission from the publisher. No patent liability is
assumed with respect to the use of the information contained
Development Editors
herein. Although every precaution has been taken in the prepa- Robin Drake
Maureen McDaniel
ration of this book, the publisher and author assume no respon-
Sarah Robbins
sibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained Managing Editor
herein. Thomas F. Hayes
International Standard Book Number: 0-7897-2562-2 Senior Editor
Susan Ross Moore
Library of Congress Catalog Card Number: 2001090372
Copy Editors
Printed in the United States of America Margaret Berson
Michael Dietsch
First Printing: December 2001
Indexer
04 03 02 01 4 3 2 1 D&G Limited, LLC

Trademarks Proofreader
D&G Limited, LLC
All terms mentioned in this book that are known to be trade-
marks or service marks have been appropriately capitalized. Technical Editor
Que cannot attest to the accuracy of this information. Use of a Vincent W. Mayfield
term in this book should not be regarded as affecting the validity Team Coordinator
of any trademark or service mark. Cindy Teeters
Media Developer
Warning and Disclaimer Michael Hunter
Every effort has been made to make this book as complete and
Interior Designer
as accurate as possible, but no warranty or fitness is implied.
Karen Ruggles
The information provided is on an “as is” basis. The author(s)
and the publisher shall have neither liability nor responsibility Cover Designer
to any person or entity with respect to any loss or damages Rader Design
arising from the information contained in this book. Page Layout
D&G Limited, LLC
00 71465_FM 11/30/01 10:26 AM Page iii

iii

Contents at a Glance
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1 Overview .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
2 An Introduction to ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
3 Migrating from ASP to ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . .31
4 The New ADO—ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
5 HTML/Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
6 Using ASP.NET List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
7 Using ASP.NET Rich Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .171
8 Using ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . . . .195
9 Using ASP.NET User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .227
10 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247
11 ASP.NET and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275
12 Custom ASP.NET Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307
13 Debugging ASP.NET Overview . . . . . . . . . . . . . . . . . . . . . . . . . . .343
14 Case Study in Ecommerce with ASP.NET . . . . . . . . . . . . . . . . . .365
A The ASP.NET Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
B The ADO.NET Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405
C VB.NET Programming Reference . . . . . . . . . . . . . . . . . . . . . . . . .459
D C# Programming Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516
00 71465_FM 11/30/01 10:26 AM Page iv

Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1 .NET Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
The Big .NET Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Class Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Runtime Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
The Common Language Runtime: A New Interpretation
of Older Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Intermediate Language and Metadata . . . . . . . . . . . . . . . . .12
The JIT Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
2 An Introduction To ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Moving to .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
A Brief History of ASP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Why ASP Developers Need This Book . . . . . . . . . . . . . . . . . . . . . .23
Benefits of ASP.NET over ASP . . . . . . . . . . . . . . . . . . . . . . . . . . .24
ASP.NET Is Compiled, not Interpreted . . . . . . . . . . . . . . . . .24
Separation of Code from Content . . . . . . . . . . . . . . . . . . . . .24
No More “DLL Hell” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Side-by-Side Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Real Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Real Programming Languages . . . . . . . . . . . . . . . . . . . . . . .25
Real Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Directory-Based Deployment . . . . . . . . . . . . . . . . . . . . . . . . .26
File-Based Application Configuration . . . . . . . . . . . . . . . . . .26
Event-Based Programming Model . . . . . . . . . . . . . . . . . . . .26
Improved, Extensible Object Model . . . . . . . . . . . . . . . . . . .27
More Integrated Functions . . . . . . . . . . . . . . . . . . . . . . . . . .27
Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Performance Improvements . . . . . . . . . . . . . . . . . . . . . . . . .28
Better Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
3 Migrating From ASP to ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . .31
Preparing to Use ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Basic Differences Between ASP and ASP.NET . . . . . . . . . . . . . . .32
00 71465_FM 11/30/01 10:26 AM Page v

Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Maintaining State Between ASP and ASP.NET . . . . . . . . . .33
Language Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Common ASP Tasks and Their ASP.NET Equivalents . . . . . . . . .36
Read a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Validate a Form’s Entries . . . . . . . . . . . . . . . . . . . . . . . . . . .42
The Login Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
View Results of Database Query . . . . . . . . . . . . . . . . . . . . . .58
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
4 The New ADO—ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
ADO vs. ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
The History of Microsoft Data Access . . . . . . . . . . . . . . . . . .66
ADO vs. ADO.NET Object Model . . . . . . . . . . . . . . . . . . . . .68
Connecting to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Using the SqlCommand Object . . . . . . . . . . . . . . . . . . . . . . . . .70
The DataReader Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
The SqlDataAdapter Object . . . . . . . . . . . . . . . . . . . . . . . . . . .75
The DataSet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
The DataGrid Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
The Repeater Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94
The DataList Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99
Working with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Generating XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
Consuming XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107
5 HTML/Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
What Are Server Controls, Anyway? . . . . . . . . . . . . . . . . . . . . . .110
Why Two Kinds of Controls? . . . . . . . . . . . . . . . . . . . . . . . .110
When Should I Use Each Kind of Control? . . . . . . . . . . . . .111
HtmlControls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111
HtmlControl Properties and Methods . . . . . . . . . . . . . . . . . .111
HtmlContainerControl Properties . . . . . . . . . . . . . . . . . . . . .114
An HtmlControl Example . . . . . . . . . . . . . . . . . . . . . . . . . .114
HtmlForm Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117
Table Control Properties . . . . . . . . . . . . . . . . . . . . . . . . . . .118
00 71465_FM 11/30/01 10:26 AM Page vi

vi

HtmlInputControl Properties . . . . . . . . . . . . . . . . . . . . . . . . .119


HtmlSelect Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
HtmlTextArea Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
HtmlAnchor Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
HtmlImage Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
HtmlInputFile Properties . . . . . . . . . . . . . . . . . . . . . . . . . . .122
HtmlInputImage Properties . . . . . . . . . . . . . . . . . . . . . . . . . .123
WebControls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
WebControl Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
HyperLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
ImageButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
LinkButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
PlaceHolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
Table, TableRow, and TableCell . . . . . . . . . . . . . . . . . . . . . . .130
TextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Populating ListBoxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Declarative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Programmatic with Items Collection . . . . . . . . . . . . . . . . . .135
Programmatic with Databinding . . . . . . . . . . . . . . . . . . . . .135
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136
6 Using ASP.NET List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .139
The ListControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
Working with DropDownList . . . . . . . . . . . . . . . . . . . . . . . . .141
Getting the SelectedItem . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Using the SelectedIndexChanged Event . . . . . . . . . . . . . . . .143
AutoPostBack: When You Just Can’t Wait . . . . . . . . . . . . . . .144
Using ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144
Using SelectedIndex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
Selecting Multiple Items with SelectionMode . . . . . . . . . . . .146
00 71465_FM 11/30/01 10:26 AM Page vii

vii

Working with the Items Collection . . . . . . . . . . . . . . . . . . . . . . . .147


Adding Items with the Add Method . . . . . . . . . . . . . . . . . . .148
Removing Items with the Remove Method . . . . . . . . . . . . . .152
Databinding and List Controls . . . . . . . . . . . . . . . . . . . . . . . . . .155
Using DataTextField and DataValueField Properties . . . . . .157
Databinding List Control Selections . . . . . . . . . . . . . . . . . .159
Changing the Layout of List Controls . . . . . . . . . . . . . . . . . . . . .161
Using RepeatLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
A Change in Direction with RepeatDirection . . . . . . . . . . . .163
Fine-Tuning List Control Layout . . . . . . . . . . . . . . . . . . . .166
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
7 Using ASP.NET Rich Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .171
Introduction to Rich Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .172
Working with AdRotator . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
Using the AdCreated Event . . . . . . . . . . . . . . . . . . . . . . . . . .178
Introduction to the Calendar Control . . . . . . . . . . . . . . . . . . . . . .182
Using SelectionMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Databinding and Calendar . . . . . . . . . . . . . . . . . . . . . . . . . .187
Modifying Calendar Display . . . . . . . . . . . . . . . . . . . . . . . . .187
Using CSS to Control Calendar Display . . . . . . . . . . . . . . . . . . . .190
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
8 Using ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . . .195
Validating HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
What Are Validation Controls? . . . . . . . . . . . . . . . . . . . . . . . . . .196
Client-Side Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197
Inserting a Validation Control . . . . . . . . . . . . . . . . . . . . . . . . . . .197
Validation Control Properties . . . . . . . . . . . . . . . . . . . . . . .199
Validation Control Methods . . . . . . . . . . . . . . . . . . . . . . . . .200
RequiredFieldValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . .200
RangeValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
CompareValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
RegularExpressionValidator Control . . . . . . . . . . . . . . . . . . . . . . .207
CustomValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213
ValidationSummary Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215
Bringing It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
00 71465_FM 11/30/01 10:26 AM Page viii

viii

One Step Beyond: VControls . . . . . . . . . . . . . . . . . . . . . . . . . . . .220


Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224
9 Using ASP.NET User Controls . . . . . . . . . . . . . . . . . . . . . . . . . .227
Introduction to User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .228
Creating ASP.NET User Controls . . . . . . . . . . . . . . . . . . . . . . . .228
Creating Code-behind User Controls . . . . . . . . . . . . . . . . .231
Defining Properties in User Controls . . . . . . . . . . . . . . . . .233
Defining Methods in User Controls . . . . . . . . . . . . . . . . . . .236
Raising Events from User Controls . . . . . . . . . . . . . . . . . . .238
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245
10 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247
HTTP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248
global.asax—A Good Place to Start and End . . . . . . . . . . .248
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249
Configuring Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . .253
What Web.config Looks Like and Where to Find It . . . . . .254
Setting up Web.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
Authentication and Authorization in web.config . . . . . . . . .258
Building a Simple Authentication Application . . . . . . . . . . . . . .260
The Application Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Deploying your ASP.NET Application . . . . . . . . . . . . . . . . . . . . .271
Assembly Deployment: \bin . . . . . . . . . . . . . . . . . . . . . . . .271
What If I Need to Update My DLL? . . . . . . . . . . . . . . . . . .271
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272
11 ASP.NET and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275
Distributed Computing: Many Things to Many People . . . . . . . .276
What Is a Web Service? . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
Coming Clean About SOAP . . . . . . . . . . . . . . . . . . . . . . . . .278
WSDL: A Service Contract Language . . . . . . . . . . . . . . . . .278
Disco and UDDI—The Discovery Protocols . . . . . . . . . . . . .279
Writing Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279
The ASP.NET Pipeline and Handler Architecture . . . . . . . .280
Writing Our First Service . . . . . . . . . . . . . . . . . . . . . . . . . .280
Processor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280
The [WebMethod] Attribute . . . . . . . . . . . . . . . . . . . . . . . . . .281
Generating a Client Proxy . . . . . . . . . . . . . . . . . . . . . . . . . .283
A Simple Web Service Client . . . . . . . . . . . . . . . . . . . . . . . .286
00 71465_FM 11/30/01 10:26 AM Page ix

ix

Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288


Returning Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . .289
Asynchronous Processing . . . . . . . . . . . . . . . . . . . . . . . . . .293
Transaction Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295
Programming Web Services with Microsoft
Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296
Creating Web Services in Visual Studio .NET . . . . . . . . . .296
Visual Studio .NET Project Structure . . . . . . . . . . . . . . . . .298
Consuming Web Services in Visual Studio .NET . . . . . . . .301
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304
12 Custom ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . .307
Custom Controls Defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
Custom Controls Versus User Controls . . . . . . . . . . . . . . . . . . . .308
Third-Party Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .309
Internet Explorer WebControls . . . . . . . . . . . . . . . . . . . . . .309
ASPSmith VControls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
SoftArtisans TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
Creating a Custom Control of Our Own . . . . . . . . . . . . . . . . . . .310
Command Line Building and Deployment . . . . . . . . . . . . .311
Visual Studio.NET Building and Deployment . . . . . . . . . . .312
Referencing the Control from ASP.NET . . . . . . . . . . . . . . .313
Building On Existing Controls . . . . . . . . . . . . . . . . . . . . . . . . . .313
Creating Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
Handling Control Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
Handling PostBacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Raising Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Databinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Building Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340
13 Debugging ASP.NET Overview . . . . . . . . . . . . . . . . . . . . . . . . . .343
ASP.NET Debugging Features . . . . . . . . . . . . . . . . . . . . . . . . . .344
System.Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344
Trace Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345
00 71465_FM 11/30/01 10:26 AM Page x

Implementing Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347


System.Trace.TraceContext . . . . . . . . . . . . . . . . . . . . . . . . . .349
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
Application-Level Debugging . . . . . . . . . . . . . . . . . . . . . . .352
System.Diagnostics Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353
Logging Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
Reading Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
Other System.Diagnostics Tools . . . . . . . . . . . . . . . . . . . . . .358
Visual Studio.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Frequently Encountered Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Runat=”Server” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Type Mismatches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
VB/VBScript Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
14 Case Study: E-commerce with ASP.NET . . . . . . . . . . . . . . . . . . .365
Building a Web Store—IBuySpy.com . . . . . . . . . . . . . . . . . . . . . . .366
IBuySpy Store Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
Design Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367
Functional Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373
Some Recommended Improvements . . . . . . . . . . . . . . . . . .382
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390
A ASP.NET Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394
Page Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395
Other Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395
Adding Code-Behind Pages . . . . . . . . . . . . . . . . . . . . . . . . .395
Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
Dealing with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
Creating a Connection with ADO.NET . . . . . . . . . . . . . . . .397
Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .398
DataSets and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399
Viewing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399
Databinding Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .400
00 71465_FM 11/30/01 10:26 AM Page xi

xi

Using Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401


Declaring a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Referencing a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
System.Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Application-Level Tracing . . . . . . . . . . . . . . . . . . . . . . . . . .402
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403
B ADO.NET Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405
System.Data Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .406
DataColumn Object Members . . . . . . . . . . . . . . . . . . . . . . . . .406
DataRow Object Members . . . . . . . . . . . . . . . . . . . . . . . . . . .409
DataSet Object Members . . . . . . . . . . . . . . . . . . . . . . . . . . .412
DataTable Object Members . . . . . . . . . . . . . . . . . . . . . . . . . .417
DataView Object Members . . . . . . . . . . . . . . . . . . . . . . . . . .421
DataViewManager Object Members . . . . . . . . . . . . . . . . . . . . .423
System.Data.OleDb Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . .425
OleDbCommand Object Members . . . . . . . . . . . . . . . . . . . . . . .425
OleDbConnection Object Members . . . . . . . . . . . . . . . . . . . . .428
OleDbDataReader Object Members . . . . . . . . . . . . . . . . . . . . .430
OleDbDataAdapter Object Members . . . . . . . . . . . . . . . . . . . .433
OleDbError Object Members . . . . . . . . . . . . . . . . . . . . . . . . .438
OleDbParameter Object Members . . . . . . . . . . . . . . . . . . . . .439
System.Data.SqlClient Namespace . . . . . . . . . . . . . . . . . . . . . . . .441
SqlCommand Object Members . . . . . . . . . . . . . . . . . . . . . . . . .440
SqlConnection Object Members . . . . . . . . . . . . . . . . . . . . . .444
SqlDataReader Object Members . . . . . . . . . . . . . . . . . . . . . .447
SqlDataAdapter Object Members . . . . . . . . . . . . . . . . . . . . .450
SqlError Object Members . . . . . . . . . . . . . . . . . . . . . . . . . .454
SqlParameter Object Members . . . . . . . . . . . . . . . . . . . . . . .455
C Visual Basic.NET Language Reference . . . . . . . . . . . . . . . . . . . .459
Changes from Previous Versions of VB . . . . . . . . . . . . . . . . . . . .459
General Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459
Procedure Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .460
Property Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .461
Changes to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462
Data Type Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .463
00 71465_FM 11/30/01 10:26 AM Page xii

xii

New Features in VB.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .464


Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .464
Structures Replace UDTs . . . . . . . . . . . . . . . . . . . . . . . . . .465
Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . .465
Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
Namespaces and Assemblies . . . . . . . . . . . . . . . . . . . . . . . .466
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Threading Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Statement Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Labeled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
Control-Flow Statements . . . . . . . . . . . . . . . . . . . . . . . . . .472
Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474
Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474
Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .476
Component-Based Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
Namespaces and Assemblies . . . . . . . . . . . . . . . . . . . . . . . .484
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485
D C# Language Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .490
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .490
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .491
Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
00 71465_FM 11/30/01 10:26 AM Page xiii

xiii

Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Statement Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Empty Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Labeled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494
Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494
Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495
jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .496
try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .497
checked and unchecked . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500
Component-Based Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512
Namespaces and Assemblies . . . . . . . . . . . . . . . . . . . . . . . .512
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .513
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516
00 71465_FM 11/30/01 10:26 AM Page xiv

xiv

About the Authors


Steven A. Smith is a Managing Consultant and Internet Specialist for
Software Architects, Inc., a Chicago-based consulting firm with offices in more
than a dozen U.S. cities. Steve has been a “SARK” for more than four years and
is currently working in the Cleveland office, focusing on highly scalable compo-
nent-based architectures. In his spare time, Steve runs the #1 ASP.NET commu-
nity site, ASPAlliance.com, and maintains a column there with dozens of articles
on ASP and ASP.NET. He has also written a number of articles for ASPToday.com
and continues to work as a technical reviewer for WROX, Prentice Hall, and
QUE. Steve has an MCSE+Internet (4.0) certification and previously completed
the MCSD. He is also a graduate of The Ohio State University with a degree in
Computer Science Engineering. His development experience prior to .NET was
mainly focused on ASP, Visual Basic, SQL Server, and COM+.
When he isn’t bonding with his laptop computer, Steve enjoys biking, swim-
ming, foosball, and spending time with his wife, Michelle, and their two dogs,
Jaegerin and Mojo.
Steve can be reached at ssmith@aspalliance.com.
Nicholas Chase has been involved in Web site development for companies such
as Lucent Technologies, Sun Microsystems, Oracle Corporation, and the Tampa
Bay Buccaneers. Nick has been a high school physics teacher, a low-level
radioactive waste facility manager, an online science fiction magazine editor, a
multimedia engineer, and an Oracle instructor. More recently, he was the Chief
Technology Officer of Site Dynamics Interactive Communications in Clearwater,
Florida, and is the author of three books on Web development, including Java
and XML From Scratch (Que). He loves to hear from readers and can be
reached at nicholas@nicholaschase.com.
Glenn Cook has been programming and teaching ASP since 1997. His tutorials
and award-winning source code continue to make his column at the AspAlliance,
the #1 ASP.NET Community, one of the most popular in its history.
He is the founder of simpleCsharp.com and is a Senior Consultant with
Fahrenheit Technology in Richmond, VA. He has lead projects for clients such as
Land Rover, Charles Schwab, The Yellow Pages, Marriot, and most recently for
CapitalOne.
When he’s not playing with code, he is spending time with his family in the
D.C./VA area.
Nathen Grass is a consultant with RDA Corporation (http://www.
rdacustomsoftware.com) in Atlanta, Georgia. RDA builds custom software
and Web-based applications to solve complex business problems for mid-market
and large-scale companies. Nathen specializes in DNA architecture development
00 71465_FM 11/30/01 10:26 AM Page xv

xv

using various Microsoft technologies and lately has been heavily involved in
learning and developing on the .NET platform. Nathen can be reached at
grass@rdacustomsoftware.com.

Vincent W. Mayfield is a Senior Software Engineer, Microsoft Certified


Solutions Developer, and Vice President/Co-Founder of Bit-Wizards IT
Solutions, Inc., of Fort Walton Beach, Florida. He has more than 12 years of
experience developing software, and over 8 years developing applications for the
Microsoft Windows Family of Operating Systems with C and C++. He has
served in the U.S. Army Reserves and the U.S. Air Force, and is an FAA
Commercial Instrument rated pilot. Vincent has authored/co-authored several
programming books to include: Wait Group’s COM/DCOM Primer Plus, ActiveX
Programming Unleashed, and Visual C++ 5.0 Developer’s Guide. Vincent holds a
BS in Mathematics with Minors in Computer Science and Aerospace Science as
well as a MS in International Relations. Bit-Wizards IT Solutions is a cutting-
edge technology company that provides professional software engineering,
e-commerce, wireless, Web, consulting, and information technology solutions.
Wynn Netherland is President of Houston, Texas-based Praexis, Inc., a soft-
ware consulting firm specializing in developing hosted applications using .NET.
Wynn has worked in Web development since 1995 and is currently working on
his second .NET title. When he’s not churning out code or chapters, he enjoys
traveling and attending Astros games with his wife, Paula. He can be reached
at wynn.netherland@praexis.com.
Scott Swigart is living proof that being a geek at age 12 pays off. He is cur-
rently a senior principal at 3 Leaf Solutions, where he spends the bulk of his
time providing training and consulting services for .NET early adopters. Scott
started working with .NET as soon as the first bits would complete an install.
In addition to working with .NET early adopters, Scott is actively involved in
various .NET projects for Microsoft.
William Wise holds a Master’s degree in Software Engineering from Brandeis
University, a Bachelor’s degree in Philosophy and Religious Studies, and has
done a fair amount of graduate work at the University of Virginia studying
Christianity and Judaism in antiquity. Professionally, he has worked as a devel-
oper, trainer, network administrator, and database administrator and has devel-
oped a number of two-tier and three-tier applications to meet the business
objectives of a variety of employers, primarily in the biotech business sector. His
interests include playing guitar, reading Tolkien’s fiction over and over, and
learning the ins and outs of Linux. William currently resides in the historic
community of Williamsburg, VA, along with his fiancé and several old, close
friends.
00 71465_FM 11/30/01 10:26 AM Page xvi

xvi

Dedication
To my wife, Michelle, with my thanks.
00 71465_FM 11/30/01 10:26 AM Page xvii

xvii

Acknowledgments
I would like to thank two Michelles in my life: my wife, Dr. Michelle Smith,
who has put up with many long hours devoted to my computer, and my editor,
Michelle Newcomb. They have both been very patient with me while I have
worked on this, my first print book. It’s been a long road with a few surprises,
and I’ve really appreciated the support. Thanks also to Chris, for many hours of
diversion while I should have been concentrating.
00 71465_FM 11/30/01 10:26 AM Page xviii

xviii

Tell Us What You Think!


As the reader of this book, you are our most important critic and commentator.
We value your opinion and want to know what we’re doing right, what we could
do better, what areas you’d like to see us publish in, and any other words of
wisdom you’re willing to pass our way.
As an Associate Publisher for Que, I welcome your comments. You can fax,
e-mail, or write me directly to let me know what you did or didn’t like about
this book—as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of
this book, and that due to the high volume of mail I receive, I might not be able
to reply to every message.
When you write, please be sure to include this book’s title and author as well as
your name and phone or fax number. I will carefully review your comments and
share them with the author and editors who worked on the book.
Fax: 317-581-4666
E-mail: feedback@quepublishing.com

Mail: Associate Publisher


Que
201 West 103rd Street
Indianapolis, IN 46290 USA
01 71465_INTRO 11/30/01 10:36 AM Page 1

Introduction

The by Example Series


How does the by Example series make you a better programmer? The by
Example series teaches programming using the best method possible. After a
concept is introduced, you’ll see one or more examples of that concept in use.
The text acts as a mentor by figuratively looking over your shoulder and show-
ing you new ways to use the concepts you just learned. The examples are
numerous. While the material is still fresh in your mind, you see example
after example demonstrating ways to use the material you’ve just learned.
The philosophy of the by Example series is simple: The best way to teach com-
puter programming is using multiple examples. Command descriptions, format
syntax, and language references are not enough to teach a newcomer a pro-
gramming language. Only by looking at many examples in which new com-
mands are immediately used and by running sample programs can
programming students get more than just a feel for the language.

How This Book Is Designed


This book is designed to quickly get you up to speed with using ASP.NET. The
Microsoft .NET Framework and ASP.NET allow you to develop powerful distrib-
uted applications using an extensive library of functions, in one of many differ-
ent programming languages. What began with Active Server Pages as a
powerful new way to develop Internet applications has evolved into ASP.NET:
a much more powerful, robust, secure, and extensible architecture.
This book is example-based. After a few introductory chapters that lay a concep-
tual foundation for the work we are going to do (and introduce you to ASP.NET),
you’ll find that every chapter is loaded with examples. I believe in the example-
based approach to learning upon which this series is based. Whenever I get into
a jam and need to learn a new programming technique, when I consult the doc-
umentation, the first thing I do is look for a good code example that fits my
needs. It’s one of the fastest ways to learn. One snippet of tight, well-
documented code is worth ten paragraphs of “how to” explanations.
To further enhance the “by Example” approach, every example in this book is
available online at the book’s online resource center. You will be able to actually
see the examples run on an ASP.NET server, as well as download the source
code.
01 71465_INTRO 11/30/01 10:36 AM Page 2

2 Introduction

Using Other Resources


The Microsoft .NET architecture encompasses nearly every part of the Microsoft
programming platform. Obviously, this book cannot hope to cover all or even
very much of the .NET architecture, of which ASP.NET is a single part. You may
find some areas of the book that refer to .NET architectural features without
much further elaboration. You can find Microsoft’s .NET documentation online
at the following URL:
http://www.microsoft.com/net/

You can also find Microsoft’s ASP.NET and Visual Studio.NET documentation
online at the following Web sites:
ASP.NET: http://www.asp.net/
VS.NET: http://msdn.microsoft.com/vstudio/

Finally, although Appendix D features a C# Reference, you can find additional


C# documentation from Microsoft at the following Web address:
http://msdn.microsoft.com/vstudio/nextgen/technology/csharpintro.asp

Who Should Use This Book


ASP.NET by Example is intended for people with some prior knowledge of
Microsoft’s Active Server Pages (ASP) technology who are familiar with pro-
gramming and dynamic data-driven Internet applications. However, you do not
need to be an expert with VBScript, JScript, or any other scripting language
because they play a very limited part in the future of ASP that is ASP.NET.
Furthermore, knowledge of ActiveX Data Objects (ADO) and knowledge of
databases in general, including some SQL (Structured Query Language), is
assumed. Some understanding of components and event-driven programming
will be helpful as well.

What We Assume You Know


We assume that you know how to develop Web pages using HyperText Markup
Language (HTML) and Active Server Pages (or a similar Web development lan-
guage). You’re familiar with either VBScript or JScript or more likely some of
both, and you’ve probably developed at least one Component Object Model
(COM) object, probably using Visual Basic. You may have used ASP 3.0 or
COM+, but many of you are still working with Internet Information Server (IIS)
4.0 on Windows NT, and will only be upgrading to Windows 2000 as this book
reaches the shelves.

What We Assume You Don’t Know


If you already know all that, what can this book do for you? Well, we assume
that you’ve heard of ASP.NET, the .NET architecture, VB Webforms, and other
01 71465_INTRO 11/30/01 10:36 AM Page 3

Introduction 3

Microsoft marketing hoopla over the last year, but that you haven’t had the
chance to work with any of it yet. We figure you’re reading this book to learn
• How ASP.NET can help you as an Internet application developer to be
more efficient and to build more powerful applications
• How the error handling in ASP.NET allows you to centralize your error
handling into one page
• How ASP.NET will make your existing applications perform better by
using compiled code
• How with ASP.NET a complete programming model is available for appli-
cations that allows for better tools and simpler form handling

This Book’s Organization


This book is organized to maximize your transition to the ASP.NET develop-
ment environment. The early chapters provide an introduction to the .NET
Framework and describe the differences between ASP.NET and its predecessor,
ASP 3.0. From there, the all-important knowledge of how to work with data
(ADO.NET) is covered early, so that you can leverage this knowledge in the
later chapters, which progressively cover more and more advanced topics.
Almost every chapter includes many examples that will help you to understand
the points being covered, and you can test the examples yourself online at the
book’s supporting Web site at http://aspauthors.com/aspnetbyexample/.
Specifically, Chapter 1 provides an overview of the .NET Framework, on which
ASP.NET is built. Chapter 2 introduces the reader to ASP.NET, and Chapter 3
provides some guidelines and examples for migrating to ASP.NET. Chapter 4
introduces the reader to ADO.NET, which is used to access data in ASP.NET.
Chapters 5 through 9 cover various controls, including List Controls, Rich
Controls, Validation Controls, User Controls, and Intrinsic Controls. In Chapter
10 we’ll look at how to manage ASP.NET applications. Chapter 11 describes
Web Services, and exciting new technology that allows separate web applica-
tions to easily communicate with one another. Chapter 12 discusses how to
build your on ASP.NET controls. Chapter 13 provides an overview of how to
debug your ASP.NET applications. Finally, Chapter 14 provides a case study
of an e-commerce site built with ASP.NET. In the appendixes you’ll find refer-
ences for the ASP.NET Object Model, the ADO.NET Object Model, the VB.NET
and C# programming languages, and a quick reference to using Visual Studio.
NET. We hope you find this useful as you continue to develop using the latest
Microsoft has to offer.
01 71465_INTRO 11/30/01 10:36 AM Page 4

4 Introduction

Conventions Used In This Book


This book uses several common conventions to help teach ASP.NET. Here is a
summary of these typographical conventions:
• Example. Examples are the most important part of any by Example title.
These provide working demonstrations of the points that are being covered
by the text in each chapter. You’ll see the Example icon beside each of
EXAMPLE those demonstrations.
• Output. The Output icon indicates a figure or code that shows the result
of the preceding example. We supply the output so that you know what to
expect when you run the example yourself.
OUTPUT • Note. Notes provide additional commentary or explanation that doesn’t fit
neatly into the surrounding text. You will find detailed explanations of how
something works, or alternate ways of doing a task.
• Tip. Tips help you work more efficiently by providing shortcuts or hints
about alternate and faster ways of accomplishing a task.
• Caution. Cautions provide a warning to you about situations that involve
possible danger to your system or to your data.
• Online Reference. These notes provide you with URLs that you can visit
to get more information or other resources relating to the topic being dis-
cussed.
• A special monospaced computer font indicates commands and computer
output.
• Because this is a book that has a limited page width, some lines of code
may be too long to fit on a single line. When you see a code continuation
character (➥), you will know that the code has wrapped to the second line.

Online Resources
In addition to the contents of this book, you will find additional resources online
at the book’s Web site. The Web site includes online sample code, any updates to
the book that may be necessary, and information for providing feedback or ques-
tions to the book’s authors. You can find the book’s support Web site at:
http://aspauthors.com/aspnetbyexample/

In addition, you will find a number of ASP.NET related Web sites on the inside
of the back cover.
01 71465_INTRO 11/30/01 10:36 AM Page 5

Introduction 5

What’s Next
OK, enough with the preliminary necessities! Let’s take a look at what all the
fuss is about with ASP.NET. In the first chapter, we’ll provide you with an intro-
duction to Microsoft’s new development architecture, the .NET Framework. This
provides the basis for all of ASP.NET (and, indeed, all Microsoft development in
the near term). Following this introduction, we’ll move into learning ASP.NET
in particular.
02 71465_CH01 11/30/01 10:12 AM Page 6
02 71465_CH01 11/30/01 10:12 AM Page 7

1
.NET Overview
“Keep on the lookout for novel ideas that others have used successfully.
Your idea has to be original only in its adaptation to the problem you’re
working on.”
—Thomas Edison
Thomas Edison did not invent the first electric light bulb or the first incan-
descent lamp. He did, however, invent the first practical and commercially
successful electric incandescent lamp. He did not invent the first electrical
power station. He improved the designs of existing generators and regula-
tors to create the first commercially successful power station capable of
delivering affordable power for electric lighting. He did not invent the tele-
graph. But he did invent the first duplex and multiplex telegraphy systems,
enabling telegraphs to send and receive messages at the same time over the
same wire.
Thomas Edison did not invent the Internet, wireless computer devices, or
even .NET. He did, however, create a framework of core technologies that
we have improved that make those technologies possible. .NET is not the
next generation of the Internet. It is the first practical set of tools that will
enable software developers to invent the next generation of the Internet.
.NET is an original adaptation of many successful and novel technologies
and ideas to solve a problem; businesses have found it very difficult and
expensive to create profitable Internet solutions with the available develop-
ment frameworks and architectures. The next generation of the Internet
will be funded by businesses who demand solutions with a short develop-
ment cycle, a quick return on their investment, long-term profitability, and
that are secure and inexpensive to maintain and upgrade.
.NET has been designed with the Internet in mind from the ground up.
.NET absorbs many successful ideas that have been in use for years, yet it
is a radical departure from the client-server framework it replaces. It is
based on time-tested, object-oriented software programming concepts, non-
proprietary standards and protocols, and programming languages that
02 71465_CH01 11/30/01 10:12 AM Page 8

8 Chapter 1: .NET Overview

many of you are already quite comfortable with. Comfortable or not,


Microsoft has decided to make a clean break from the past, which means
that nobody is completely immune from a learning curve if they care to fol-
low Microsoft into the future. If you already own a copy of Windows 2000,
or Windows XP, you will not need to purchase anything more from
Microsoft to develop, test, and deploy your own software inventions on the
.NET framework. The only thing you will need to invest in beyond that is
your future.
If you don’t buy into the hype, good for you! We encourage you to look
beyond our biased enthusiasm to examine all the facts. When you gain a
better understanding of what .NET is, and the role that ASP.NET has
within this platform, most of you will recognize the changes and invest-
ments that you will need to make to create your own personal commercial
success. The changes might even force you to take a closer look at what the
competition offers, which can’t hurt, regardless of your opinions about
.NET. If you’re starting to feel a bit overwhelmed, just look to the closest
light socket and a bit of Edison’s wisdom to guide you: “Opportunity is often
missed because it is dressed in overalls and looks a lot like work.”
This chapter will give you a high-level introduction to the key concepts and
components that make up the .NET Framework. You will have a much
shorter learning curve if you can think of ASP.NET in terms of the big
.NET picture. We know you’re anxious to dig into the fun stuff, so we’ll do
our best to keep this chapter as quick, fun, and rewarding as we can.

The Big .NET Picture


.NET is a software development and application execution environment
that allows you to create, compile, test, deploy, and execute software that
may be coded in a variety of different programming languages that adhere
to a single set of Common Language Runtime files. In .NET’s initial public
release, Microsoft presents new versions of its most popular programming
languages: Visual Basic and C++, which are now called Visual Basic .NET
and MC++ (Managed C++). Although Microsoft has retired J++, they intro-
duce two new languages: J# and C# (pronounced j-sharp and c-sharp
respectively). In terms of their respective performance and power, all of
these languages are very similar. In terms of the syntax they use, VB.NET
is the only programming language that is not derived from the C Language.
Despite the subtle differences between them and their individual compilers,
the resulting compiled code is the same for every language. This insures
completely seamless language interoperability on a common integrated
platform that is supported by the .NET Framework’s Class Library and
Runtime. Microsoft bundles support for all of these new languages in its
02 71465_CH01 11/30/01 10:12 AM Page 9

The .NET Framework 9

latest release of Visual Studio, now called Visual Studio.NET (VS.NET).


Microsoft’s entire product line of development packages has undergone sig-
nificant changes to satisfy the requirements of this new framework. Of
course, Microsoft would love it if you decided to buy Visual Studio .NET to
develop your .NET applications, but .NET’s open architecture is also an
invitation to any programming language and development environment. In
other words, the output of PASCAL’s compiler will look no different than
the output produced by C#, Eiffel, COBOL, Visual Basic .NET, and so on.
As long as a programming language has a compiler that adheres to the
.NET Framework’s extremely strict set of rules to produce a common exe-
cutable language, it is a welcome addition to the .NET family.

The .NET Framework


Currently, the .NET framework will run on the following operating sys-
tems: Windows 98, Windows ME, Windows 2000, and Windows XP. (We
expect that this list of operating systems will eventually include third-party
O/Ss, so we recommend that you check “http://msdn.microsoft.com/net/”
to check for any new additions to that list.)
From a high-level view, the .NET Framework can be described as a little
virtual operating system that runs on top of one of the operating systems
we mentioned. A closer inspection would reveal that the framework is made
up of two main components: the .NET Class Library and the Common
Language Runtime (CLR).

Class Orientation
The .NET Class Library is a huge organized hierarchy of class objects,
shown in Figure 1.1. These objects expose services that you can use to
develop your own services. They include support for things like Windows
Forms and Web Services, as well as objects for working with XML and data.
To include these services in our applications, we navigate the hierarchy
using traditional object-oriented programming principles. Navigating this
hierarchy is a lot like you would navigate hierarchy of files and folders on
your hard drive. For example, if you were referring me to a certain SQL
driver on my system (assuming we have the same OS) you would use some-
thing like “c:\WINNT\System\Data\SQLClient\”. A similar reference
included in your code would look like “System.Data.SQLClient”. The only
difference is that the object-oriented code references separate each level of
the hierarchy with a dot “.” These explicit references to groups of classes
within the Framework’s class libraries are also referred to as namespaces
in .NET. You can think of namespaces as organizing classes just as folders
organize files in a file system.
02 71465_CH01 11/30/01 10:13 AM Page 10

10 Chapter 1: .NET Overview

Windows Windows
Web HTTP
Pages Forms Service
Services Handlers
Applications Applications

ASP.NET

System Class Library

System Data Web XML More…

Common Language Runtime

.NET Framework

OS

Figure 1.1: The .NET Framework’s Architecture.

A namespace is a unique hierarchical reference to a specific class or group


of similar classes. For example, the base classes that provide the services
that support the runtime environment are found in the System namespace.
This namespace includes services for things like I/O, security, data, and
Web-related operations. To access these namespaces programmatically, your
reference would look like System.Web or System.Data. The more specific a
namespace is, the more specific its services will usually be. For example, if
I wanted to connect to an SQL database, I could use the System.Data.
SqlClient namespace.

We also use the benefits of a unique class reference so that two objects with
the same name cannot clash. Imagine that you have an application that
allows remote clients to call Web services in your Customers.Customer
object. If that remote client has a local reference to its own version of an
object named Customers.Customer, there will be some major problems for
that remote client. Microsoft recommends that each developer create at
least two unique namespace levels that become the root references for all
their object creations. For example, a much better reference to my applica-
tion’s “Customer class” might look something like this:

“JoesGarage.GlennCook.SimpleCSharp.ASPWebServiceApp.Customers.Customer”

Runtime Hosts
EXAMPLE The last component in the framework that at least deserves an honorable
mention as a “main component” (especially in this book) is a .NET runtime
02 71465_CH01 11/30/01 10:13 AM Page 11

The .NET Framework 11

host. A runtime host is an application that loads the .NET runtime into its
process. ASP.NET is an example of an application that loads the runtime
into its process for its Web-related services. Internet Explorer is another
example of a runtime host allowing us to download and execute managed
code within its processes. The last runtime host component included in the
framework is shell executables. This piece is actually what calls the .NET
runtime from your operating system when you want to start a .NET appli-
cation. It will also transfer the control of the .NET application from your
system to the runtime.

The Common Language Runtime: A New Interpretation


of Older Technology
The Common Language Runtime has many similarities to the runtime
environment that executes Java applications. You’ll learn later on that
there are just as many differences as there are similarities, but it provides
a good frame of reference for learning about a managed code execution
environment.
To understand how the runtime works, let’s discuss it in terms of an imagi-
nary Web Service application you have just finished coding within Visual
Studio .NET. VS .NET will handle all the dirty details of what needs to be
passed to the appropriate compiler for your code. In earlier versions of
Visual Studio, each language had its own similar-looking IDE, but was a
separate application. In VS .NET, you have one unified development envi-
ronment that can call a number of different languages’ compilers.
Alternatively, you can pass these specific command-line arguments to the
compiler on your own using the appropriate syntax at the command-line
prompt. This will involve a bit more research and studying on your part,
but if you insist on this approach, at least do yourself a favor and download
an evaluation version of VS .NET. If nothing else, the evaluation version
will help you learn what it will pass to the compiler along with your code.
In order to compile a simple C# application, a command line call to the
compiler might look like:

“c:/>csc.exe simpleCSharp.cs”

Each programming language has its own compiler. Every one of these com-
pilers must adhere to a common set of strict rules found at the core of the
EXAMPLE CLR. These strict rules ensure that each language’s compiler will produce
the same type of compiled code. Even though your compiled .NET applica-
tion will have the same extension as a traditional Win32-based executable
(that is, EXE or DLL), the internal results of the files are completely differ-
ent. A .NET executable is compiled into what could be described as an “exe-
cutable package” that includes Microsoft’s Intermediate Language (MSIL or
02 71465_CH01 11/30/01 10:13 AM Page 12

12 Chapter 1: .NET Overview

IL), metadata, an assembly manifest, and any additional assemblies or files


the application makes reference to. In .NET, your application’s executable is
more commonly referred to as an assembly. An assembly is the compiled
code that you will distribute to clients.
Before we discuss what happens to the assembly when a client launches
the application, let’s take a closer look at the internal components that
make up a .NET assembly.

Intermediate Language and Metadata


Intermediate Language (IL) is .NET’s version of compiled code. Whether
you are compiling an ASP.NET DLL written in COBOL, or a Windows
Forms EXE written in C#, the result is always this common self-describing
intermediate language. IL is a simple text-based syntax that is comple-
mented by a self-describing component called metadata. The combination of
these two technologies gives .NET’s managed runtime the ability to perform
more operations in less time with less overhead.
The similarities between .NET and Java’s managed runtime model will
often draw comparisons with .NET’s intermediate language and Java’s byte
code. This similarity comes from the benefits of a common compiled code
that is capable of running on any machine that supports their respective
runtime environment. In terms of their form and function, however, the two
are completely different. .NET’s compiled code (IL and metadata) offers
some significant advantages over Java’s integer-based byte code compila-
tions.
.NET applications are Just-In-Time compiled a second time into native
machine code. This is the same type of machine language code a 32-bit C++
executable would be compiled into. An assembly’s descriptive text-based
syntax allows the runtime the ability to intelligently compile an application
into the most efficient set of native machine instructions for a given system.
The .NET runtime also gives the developer compilation options to instruct
the runtime to dynamically Just-In-Time (JIT) compile their application to
machine language, or pre-compile everything to native machine code during
installation. For example, if you want to avoid the overhead involved with
JIT compiling, you can instruct the runtime to compile your application to
native machine code during its initial set-up and installation process.
Java applications are compiled into an integer-based syntax that is never
compiled into native machine code. Instead, the Java runtime uses a
process that interprets its byte code compilations dynamically. Since an
interpreter is essentially software that emulates a CPU, there is additional
overhead in supporting this type of design.
02 71465_CH01 11/30/01 10:13 AM Page 13

The .NET Framework 13

In terms of performance differences, a recent benchmark comparison of the


J2EE vs .NET (released by Microsoft while .NET was still in Beta) demon-
strated that .NET greatly outperforms J2EE at almost every level while
using only a fraction of the CPU’s resources that J2EE required.
.NET precompiles its applications into native machine language with the
help of an assembly’s metadata. Metadata is XML that thoroughly
describes the location and purpose of every object, property, argument, dele-
gate, and method within a .NET application’s assembly. Metadata elimi-
nates the overhead associated with interpreting and managing the
unknown. The runtime also uses metadata to validate the accuracy and
purpose of each function to avoid errors, optimize memory management,
and protect the user from malicious attacks.
Figure 1.2 shows the process of compiling an application into an assembly.
While coded primarily in C#, you’ll notice it includes references to files
coded in many other languages.
.NET’s compiled code also addresses something commonly referred to as
“DLL Hell.” In the past, developers would have to tweak and build multiple
versions of their Win32 applications to create compatible executables for
each category of popular system configurations. All it would take to destroy
all your brilliant efforts is for another application to upgrade a shared sys-
tem DLL to a newer version. .NET developers will not have to struggle with
these issues any more, but .NET has costs associated with it that Win32
apps don’t have. In other words, you should not expect your managed .NET
applications to run as quickly as similar unmanaged Win32 applications.
Microsoft’s stance is that IL’s self-describing code will eventually allow the
runtime to compile native code so specific to an end user’s system that it
will easily be able to outperform Win32 applications. And of course,
ASP.NET runs several times faster than classic ASP, so it is by no means
slow.
The true inner beauty and power of every compiled .NET application is its
Metadata. It is a “novel” solution that preserves the benefits of a managed
code environment but uses new technology to overcome the performance
losses associated with it. The idea is that the compiled code should read
like a novel but has no room for interpretation, ambiguity, and assump-
tions: “Nothing but the facts, please.” This enables the CLR to quickly com-
pile (yes, there is a second compilation) and manage the safest and fastest
execution environment possible.
Metadata is really nothing more than XML code that describes the entire
contents of the assembly. When a user starts a .NET application, the run-
time will access the assembly manifest, which is made up of metadata that
02 71465_CH01 11/30/01 10:13 AM Page 14

14 Chapter 1: .NET Overview

Windows Form

Class
assemblies

C# .NET Application
images in VS.NET

X COM+dll

HTML

.NET CODE COMPILER


CSC.EXE
(C# Compiler)

Compiling Assembly

Metadata
IL Code

simpleCsharpAssembly.dll

Figure 1.2: Compiling a .NET application into an assembly.


describes the specific contents of the package. It’s like the table of contents
for a .NET application. The manifest is also the runtime’s main entry point
for gathering and examining the data it needs to begin compiling the
assembly into native machine language. You can also access this informa-
tion via a disassembler included with the framework called ILDASM.EXE.
This tool presents the high-level details of an assembly in a readable and
organized hierarchy that describes the specific details of every object,
02 71465_CH01 11/30/01 10:13 AM Page 15

Summary 15

method, argument, and property in your application. If you haven’t done


your homework, you might think this is Microsoft’s consummate security
blunder. Without digging into the specific details right now, suffice to say
that this is not a blunder, and the intellectual integrity of an application
targeted for the .NET Framework is no more or less vulnerable to hackers
than an executable compiled for any other platform.

The JIT Compiler


Another critical component in the .NET Framework is the JIT (Just-In-
Time) compiler (also known as “the JIT-ter”). This is the piece that actually
compiles the contents of the assembly into machine language. It will not
compile the entire assembly into memory all at once. Its initial responsibil-
ity is to examine the manifest to identify the critical pieces to compile and
load to ensure that the application opens quickly. Beyond that point, the
JIT compiler will only load the pieces of the application that it needs. The
final product of this “just in time” approach is a runtime environment with
more available memory space to process what is being used, faster.
Microsoft believes that this approach will also guarantee that .NET appli-
cations will be able to outperform their much older, unmanaged, and occa-
sionally delinquent cousin, the Win32 executable.
Let’s take a look at components within the Common Language Runtime
that prepare, compile, and manage a .NET application.

Summary
The .NET Framework is the infrastructure that supports an integrated
development and execution platform that allows seamless interoperability
between programs developed in a variety of languages. The framework is
governed by a common yet strict set of standards, which are enforced by the
Common Language Runtime (CLR). These rules are the foundation of
.NET’s multilanguage interoperability.
The output of a compiled .NET executable is called an assembly. An assem-
bly is made up of MSIL code and metadata. This is the package that is
delivered to the end user, which is compiled a second time into native
machine language when the user starts the application. An assembly has a
.DLL extension, but is different from a traditional Windows DLL.
Intermediate Language (IL) is an optimized compiled code developed by
Microsoft that .NET’s “jitter” (JIT) uses to compile into native machine
code.
02 71465_CH01 11/30/01 10:13 AM Page 16

16 Chapter 1: .NET Overview

Common Language Runtime (CLR)

Managed Code Execution

NATIVE MACHINE CODE

Microsoft
JIT COMPILER .net
VERIFIER

LOADER

Metadata
IL Code

XML

ASP.NET Web X
Service coded with
VB.NET and HTML Compiled
source code:
C# Class C++ C# Windows form intermediate
Libraries DCOM dll GUI Language (IL)

Figure 1.3: Compiling an assembly into native machine code for execution
within the Common Language Runtime.

An assembly’s intermediate language contains descriptive XML code that


gives the CLR detailed information about the entire application. It gives
the JIT compiler a virtual road map of your application so that the native
code can provide the runtime with information about shortcuts during rush
hour, the history of the area, unsafe areas to avoid, and when it should put
out the garbage.
• The operating systems and servers that support the framework
• The compiled applications and Web services that will run within the
framework
• The .NET Framework and its Common Language Runtime
02 71465_CH01 11/30/01 10:13 AM Page 17

What’s Next? 17

• New services yet to be invented


• Nonproprietary communication protocols like XML and SOAP for
exchanging data and services

What’s Next?
In the next chapter, we will see how ASP.NET fits into the .NET architec-
ture, what some of its most exciting features are, and how it differs from
classic ASP. By the end of the chapter, if you’re currently building web sites
using ASP, you should be eager to make the jump to ASP.NET and start
taking advantage of all of the benefits it has over ASP.
03 71465_CH02 11/30/01 10:31 AM Page 18
03 71465_CH02 11/30/01 10:31 AM Page 19

2
An Introduction To ASP.NET
Microsoft Active Server Pages (ASP) technology has evolved once more.
This time, the changes Microsoft has made are so great that they have cho-
sen not to make another point release of ASP (to 4.0), but have labeled this
next generation of their Internet technology ASP.NET. ASP.NET (formerly
Next Generation Web Services or ASP+) and the .NET framework will
change the way developers build powerful Internet applications more than
any prior evolution of ASP before it. Although entire books could be written
about the .NET framework (and, as you read this, I’m sure they have been),
we can only cover some of the major points about this new architecture
before jumping into specific ASP.NET examples.
In this chapter you will
• Take a look at a brief history of Active Server Pages (ASP)
• Learn what ASP.NET is
• Compare the benefits ASP.NET with those of “Classic ASP”
03 71465_CH02 11/30/01 10:31 AM Page 20

20 Chapter 2: An Introduction To ASP.NET

Moving to .NET
What is .NET? .NET is Microsoft’s new programming architecture, and is
an effort to unify development so that true language independence can be
achieved. Further, .NET is designed for the disconnected Internet program-
ming model, rather than the traditional two-tier client/server model that
Distributed Component Object Model (DCOM) and other earlier models
supported. ASP.NET is just one piece of this new architecture, and of course
is the focus of this book.
What does ASP.NET do for you, the developer? Well, let’s look at a list of 15
cool new things about ASP.NET as compared to ASP 3.0. This comparison is
not meant to be complete, because this is just an introduction to ASP.NET,
but it should give you a good idea of why ASP.NET is far superior to its pre-
decessor. So, in no particular order, here are some of the major improve-
ments. Don’t worry if there are some items listed that are unclear—we will
cover each point in more detail before the end of the chapter.
• Compiled code, not interpreted
• Separation of code from content
• No more “DLL hell”
• Side-by-side installation
• Real debugging
• Real programming languages
• Real error handling
• Applications can be deployed with XCOPY or FTP
• Applications can be administered with XML-structured configuration
files
• Event-based programming model
• Improved object model
• More integrated functions, such as form validation and integral form
handling
• Web Services
• Great performance enhancements from features like page caching
• Better tools—Visual Studio .NET rules!
03 71465_CH02 11/30/01 10:31 AM Page 21

A Brief History of ASP 21

This chapter will provide a brief look at the background of ASP.NET and
how it has evolved from ASP. I’ll explain the key differences between ASP
3.0 and ASP.NET in an overview fashion, and issues such as migrating
from “classic ASP” to ASP.NET will be covered in later chapters.

A Brief History of ASP


When Active Server Pages was first released in November of 1996, four
years ago as this book is going to press, it provided an easy way to create
dynamic Web pages. Although Common Gateway Interface (CGI) and Perl
were very popular at the time, ASP quickly gained popularity for four rea-
sons. These included ease of data access, ease of page design, Component
Object Model (COM) interoperability, and a relatively flat learning curve for
many programmers already familiar with Visual Basic.
For more information about the origins of ASP, originally code-named
Denali, see http://msdn.microsoft.com/library/periodic/period96/
Denali.htm.

First, if ASP had not been released with ActiveX Data Objects (ADO), it
most likely would not have gained in popularity as quickly as it did. ADO
replaced Remote Data Objects (RDO) and later Data Access Objects (DAO)
as Microsoft’s preferred method of accessing databases, and it provided a
simple, straightforward object model.
Second, with the release of ASP 1.0 came the first Microsoft Visual Studio
application suite, which includes Visual InterDev 1.0. This tool provided
many features, especially for a 1.0 product, and took advantage of
Microsoft’s FrontPage Server Extensions (FPSE) to allow easy maintenance
of Web sites without the need for a separate FTP client. Intellisense for
COM and built-in ASP objects as well as a data access control made this a
very popular editor despite its shortcomings, such as its Graphical User
Interface (GUI) editor, which relied on an early version of FrontPage.
It is probably safe to say that if it were not for ASP, the third-party compo-
nent market would not be the multimillion dollar per year industry that it
is today. Certainly this was not the only factor, as third-party controls for
Visual Basic predated the COM revolution, but in my opinion, ASP and
COM have allowed some businesses to become very profitable by selling
pre-packaged components for use on web servers. The ability to purchase
off-the-shelf components and plug them into a Web site simply did not exist
for all but the most skilled of programmers prior to the release of ASP 1.0.
By bringing this capability to the masses, ASP opened up an entirely new
market for component vendors, who continue to provide powerful off-the-
shelf tools that are easily integrated into ASP applications.
03 71465_CH02 11/30/01 10:31 AM Page 22

22 Chapter 2: An Introduction To ASP.NET

The fourth feature of ASP that sparked its popularity was its use of
VBScript as its default language. This allowed the many programmers who
were already familiar with Visual Basic to quickly jump into ASP program-
ming with minimal learning required. For the many programmers familiar
with JavaScript, or the syntactically similar C, JScript was offered as well.
In fact, Microsoft provided the ability to use other third-party languages as
well, and it wasn’t long before one could write ASP code in Perl.
As cool as it was, ASP 1.0 had some significant limitations. One major limi-
tation for anyone working with COM components was that the Web server
needed to be restarted whenever a Dynamic Link Library (DLL) needed to
be updated (A COM object is stored as a .DLL file). Other security and per-
formance issues were addressed, but the largest improvements from ASP
1.0 to 2.0 came in the form of Microsoft Transaction Server (MTS). ASP 2.0
shipped as part of Internet Information Server (IIS) 4.0 and MTS 1.0
shipped as part of the free Windows NT 4 Option Pack.
With IIS 4.0, Microsoft introduced the new Microsoft Management Console
(MMC), which was used to administer both IIS 4.0 and MTS. MTS made
life much easier for anyone developing or using COM components. It han-
dled installation and uninstallation of the components, alleviating the need
to restart the Web service (or often the server itself), and also removed a lot
of the burden of handling transactions from the developer. In addition, it
acted as an object broker, effectively caching object instances and brokering
them out on request. This technique led to the current focus on “stateless”
components because this was necessary for such object reuse to be effective.
In addition, newer versions of ADO further enhanced developers’ abilities to
work with remote data, using new techniques like XML streams, among
other things.
In February 2000, Microsoft released IIS 5.0 with Windows 2000. With IIS
5.0, ASP was in version 3.0, and MTS was replaced with COM+ services.
COM+ essentially combined the functionality of MTS with message queuing
services, and ASP gained some additional functionality, including a few
more methods and intrinsic object properties. On the whole, the major dif-
ferences between programming in ASP 2.0 and ASP 3.0 lay in the support-
ing services like COM+ more than in the language itself. Anyone who could
write VBScript in ASP 1.0 with Visual InterDev 1.0 would still be perfectly
at home using IIS 4.0 or IIS 5.0. This simply is not the case with ASP.NET.
Microsoft employees Mark Anders and Scott Guthrie began developing
what would become ASP.NET in early January of 1998. At this time, ASP
was just a year old, but some of its limitations were already quite evident.
03 71465_CH02 11/30/01 10:31 AM Page 23

Why ASP Developers Need This Book 23

For instance, the restriction to scripting languages and the lack of a compo-
nent model for ASP made it difficult to develop good tools for it. The inter-
spersing of code with output HyperText Markup Language (HTML)
frequently resulted in problems when designers and developers worked
together on the same project. ASP.NET was designed from the start to
address the limitations of ASP and overcome them.
Mark and Scott chose to build ASP.NET, then called ASP+, on the Next
Generation Web Services (NGWS) Runtime that was then in development.
NGWS, which would become .NET, provided a rich set of programming
libraries, and would soon include the new language C#, in which ASP.NET
itself is written. At this time, ASP.NET has been in development for over
three years, and Microsoft’s focus with this product remains on these
priorities:
• Factored design. ASP.NET is written as a set of modular compo-
nents that can be replaced or extended as needed.
• Scalability. Great efforts were made to build a highly scalable model,
especially with regard to maintaining state.
• Availability. ASP.NET has been designed to intelligently detect
crashes, memory leaks, and deadlocks and to recover from these
events gracefully.
• Performance. ASP.NET takes advantage of compiled languages and
early binding to improve performance, and also features extensive
caching support.
• Tools integration. Microsoft’s goal is to make building a Web site as
easy as building a form using Visual Basic. Visual Studio .NET is the
first tool to provide this functionality, but the other vendors are sure
to follow with their own toolsets.

Why ASP Developers Need This Book


If you have read the last couple of pages, you understand that the evolution
of ASP has primarily involved changes in peripheral services, not the core
ASP engine itself. ASP.NET, previously known as Next Generation Web
Services, is a truly different creature than ASP, and will require you to
rethink how you go about designing and building Internet applications.
Why should you bother to learn this new way of doing things when you
already build great applications using classic ASP? What are the benefits
that make it worth it? Or, if you’re just getting into web development, why
should you choose ASP.NET instead of the more established Active Server
Pages technology?
03 71465_CH02 11/30/01 10:31 AM Page 24

24 Chapter 2: An Introduction To ASP.NET

Benefits of ASP.NET over ASP


ASP.NET and the .NET Framework feature many advantages over classic
ASP. ASP.NET outperforms ASP, and is more robust, secure, and scalable.
It has better tools, allowing programmers to be more productive, and it will
support many different languages, allowing developers to use whichever
one they prefer. ASP.NET will also be easier to manage and deploy. Let’s
expand on our list of advantages of ASP.NET over ASP that were men-
tioned at the start of this chapter.

ASP.NET Is Compiled, not Interpreted


Compiled programs run faster than interpreted ones. Thus, ASP.NET,
which is compiled, runs faster than classic ASP, which is interpreted. Each
page is compiled the first time it is requested, and then the compiled code
of the page is kept until that page is changed or the application is
restarted. Optionally, files can be precompiled at deployment time, to
reduce latency when pages are first accessed.

Separation of Code from Content


ASP.NET will allow true separation of code from presentation, which will
allow graphic designers and programmers to work together with less frus-
tration and time spent merging pages’ looks with their functionality. This is
done through the use of “code behind” pages, which are referenced using a
page directive in the header of the page with the presentation code.

No More “DLL Hell”


Users and developers of COM components have come to refer to the prob-
lems with COM deployment as “DLL Hell.” This means that installing or
moving COM components often breaks dependent applications without
warning. Otherwise stable applications are frequently broken when a new
application updates an existing component. “DLL Hell” exists because the
COM protocol requires that components do not change the interfaces they
expose, and so any time a component’s interface changes, it gets a new
identifier that basically makes it a new version of the component. Programs
that relied on one version of a component are frequently broken when they
try to communicate with a newer version of that component. If you have
ever installed a new program on your computer only to find that some of
your other programs no longer work afterward, you have experienced “DLL
Hell.”
With ASP.NET, components don’t have to be shared across the server, but
can be placed with individual applications. Also, components are kept with
03 71465_CH02 11/30/01 10:31 AM Page 25

Benefits of ASP.NET over ASP 25

the application, and the entire application can be moved using simple file
copying. No registry changes or dealing with MTS/COM+ is necessary! This
makes it very easy to maintain an ASP.NET application remotely, such as
through a Web hosting provider. Of course, components can still be shared,
but that decision is now left to the developer or administrator.

Side-by-Side Installation
If you’re not sure you’re ready to migrate your production applications to
ASP.NET yet, don’t worry. The new services and features can be installed
and running in parallel with your existing classic ASP applications (on IIS
4 or 5). In fact, they can share the same folder structure—all that you need
do to migrate each file (after you’ve made it utilize the new features of
ASP.NET) is change the file extension from .asp to .aspx (and of course
update your links to this file accordingly). You will literally be able to
migrate your applications a page at a time.

Real Debugging
ASP.NET features easier debugging than did Classic ASP (which isn’t say-
ing much). One simple addition is a trace command that is only compiled
into the running code when a compile flag is set. No need to build your own
debugging code using if-then and Response.Write. Also, with Visual Studio
.NET, you can step through your ASP.NET code, your include files and Web
controls, and your .NET components, even if each of these uses a different
programming language, without being on the server. It is real debugging,
just as with any other Visual tool!

Real Programming Languages


Although ASP supports several scripting languages, ASP.NET (and in fact
the .NET Framework) will support any language that can be compiled to its
intermediate language (IL) format, which at the time of printing includes
over 16 different languages, including Ada, APL, COBOL, C#, Eiffel,
Haskell, Jscript, ML, Oberon, OZ, Pascal, Perl, Python, Scheme, Smalltalk,
VB, and others. Similar to Java (don’t tell anyone), ASP.NET languages are
compiled to a standard format (the Intermediate Language, or IL) that the
.NET architecture then compiles and executes with the Common Language
Runtime (CLR). Note that there are quite a few differences between the
implementation of .NET and Java, but they are beyond the scope of this
book to cover. Microsoft .NET’s language independence means that develop-
ers can use whatever programming language they feel most comfortable
with and won’t need to learn a new language to learn ASP.NET.
03 71465_CH02 11/30/01 10:31 AM Page 26

26 Chapter 2: An Introduction To ASP.NET

Real Error Handling


ASP.NET features better error handling, including the ability to send all
programming errors to a single error handler page, transferring all of the
page’s attributes (like Request and other variables) as well. Having a cen-
tral location to manage errors is a vast improvement over having to check
for errors every few lines of VBScript and write a custom error handler for
each case.
Further, Visual Basic.NET now supports the Try...Catch structure familiar
to Java and JavaScript programmers. Although On Error Resume Next is
still supported, it is not recommended and should be replaced with
Try...Catch blocks, which offer greater flexibility and better cross
language compatibility.

Directory-Based Deployment
Migrating an ASP application from one server to another is a daunting
task. FrontPage extensions, COM components, and Web settings are all
separate from the actual files in the directory to be moved. With ASP.NET,
you can deploy your application, complete with server settings and compo-
nents, using XCOPY or FTP. This makes backing up a site much easier, and
eliminates a lot of the hassle involved with remote Web hosting.
Once, in the days of MS-DOS, programs were as easy to install, move, and
uninstall as directories were to copy. Moving the files moved the program.
With the advent of Windows and the notorious system registry, this simplic-
ity was lost. Now, with .NET, we can once again install entire applications
using nothing more than a file manager or FTP client.

File-Based Application Configuration


Administering the application can be done entirely via XML configuration
files—no need for direct access to the server! Again, this makes remote
maintenance much easier than with classic ASP. And because the configu-
ration files use a standard XML format, you can expand them to include
your own application-specific settings as well, and third-party administra-
tion tools can also be written to manage ASP.NET applications by using
these files. The current beta of Visual Studio .NET does not offer a graphi-
cal means of maintaining these settings, but one is likely to be released in a
later version of VS.NET.

Event-Based Programming Model


ASP pages are simple scripts that begin execution at the top of the file and
continue line by line down the page until the script has completed. By con-
trast, ASP.NET pages feature an event-based programming model, which
Random documents with unrelated
content Scribd suggests to you:
THE CRIMES OF RICHARD HAWKINS.

By Thomas Aird.

When a young man, Richard Hawkins was guilty of the heinous


crime of betraying the daughter of a respectable farmer in the west of
Galloway, of the name of Emily Robson. As he yet loved the injured
maiden, he would have married her, but in this he was determinedly
opposed by her relatives, and particularly by her only brother,
betwixt whom and himself an inveterate hostility had, from various
causes, been growing up since their earliest boyhood. From remorse
partly, and shame and disappointment, and partly from other causes,
Hawkins hereupon left his home and went abroad; but after making
a considerable sum of money he returned to Scotland, determined to
use every remonstrance to win over Emily’s friends to allow him yet
by marriage to make reparation to the gentle maiden, the
remembrance of whose beauty and faithful confiding spirit had
unceasingly haunted him in a foreign land. He arrived first at
Glasgow, and proceeded thence to Edinburgh, where he purposed to
stay a week or a fortnight before going southward to his native
county, in which also Emily Robson resided.
During his stay in the metropolis, having been one evening invited
to sup at the house of a gentleman, originally from the same county
with himself, scarcely had he taken his seat in his host’s parlour,
when Emily’s brother entered, and, instantly recognizing him,
advanced with a face of grim wrath, denounced him as a villain,
declared he would not sit a moment in his company, and to make
good his declaration, instantly turned on his heel and left the house.
The violent spirit of Hawkins was in a moment stung to madness by
this rash and unseasonable insolence, which was offered him,
moreover, before a number of gentlemen; he rose, craved their leave
for a moment, that he might follow, and show Mr Robson his
mistake; and sallying out of the house, without his hat, he overtook
his aggressor on the street, tapped him on the shoulder, and thus
bespoke him, with a grim smile:—“Why, sir, give me leave to
propound to you that this same word and exit of yours are most
preciously insolent. With your leave, now, I must have you back,
gently to unsay me a word or two; or, by heaven! this night your
blood shall wash out the imputation!”
“This hour—this hour!” replied Robson, in a hoarse compressed
whisper; “my soul craves to grapple with you, and put our mutual
affair to a mortal arbitrament. Hark ye, Hawkins, you are a stranger
in this city, I presume, and cannot reasonably be expected easily to
provide yourself with a second; moreover, no one would back such a
villain;—now, will you follow me this moment to my lodgings, accept
from my hand one of a pair of pistols, and let us, without farther
formality, retire to a convenient place, and do ourselves a pleasure
and a justice. I am weary of living under the same sun with you, and
if I can shed your foul blood beneath yon chaste stars of God, I would
willingly die for it. Dare you follow me?—and, quickly, before those
fellows think of looking after us?”
To Hawkins’ boiling heart of indignation ’twas no hard task so to
follow, and the above proposal of Robson was strictly and instantly
followed up. We must notice here particularly, that, as the parties
were about to leave the house, a letter was put into Robson’s hand,
who, seeing that it was from his mother, and bore the outward
notification of mourning, craved Hawkins’ permission to read it,
which he did with a twinkling in his eye, and a working, as of deep
grief, in the muscles of his face; but in a minute he violently crushed
the letter, put it into his pocket, and, turning anew to his foe with
glaring eyes of anger, told him that all was ready. And now we shall
only state generally, that within an hour from the first provocation of
the evening, this mortal and irregular duel was settled, and left
Robson shot through the body by his antagonist.
No sooner did Hawkins see him fall, than horror and remorse for
his deed rushed upon him; he ran to the prostrate youth, attempted
to raise him up, but dared not offer pity or ask forgiveness, for which
his soul yet panted. The wounded man rejected his assistance—
waved him off, and thus faintly but fearfully spoke:—“Now, mine
enemy! I will tell you, that you may sooner know the curse of God,
which shall for ever cling and warp itself round all the red cords of
your heart. That letter from my mother, which you saw me read, told
me of the death of that sister Emily whom I so loved; whom you—oh,
God!—who never recovered from your villany. And my father, too!—
Off, fiend, nor mock me! You shall not so triumph—you shall not see
me die!” So saying, the wounded youth, who was lying on his back,
with his pale writhen features upturned, and dimly seen in the
twilight, with a convulsive effort now threw himself round, with his
face upon the grass.
In a fearful agony stood Hawkins, twisting his hands, not knowing
whether again to attempt raising his victim, or to run to the city for a
surgeon. The former he at length did, and found no resistance; for,
alas! the unhappy youth was dead. The appearance of two or three
individuals now making towards the bloody spot, which was near the
suburbs of the town, and to which, in all probability, they had been
drawn by the report of the pistols, roused Hawkins, for the first time,
to a sense of his own danger. He quickly left the ground, dashed
through the fields, and, without distinctly calculating his route,
instinctively turned towards his native district.
As he proceeded onwards, he began to consider the bearings of his
difficult situation, and at last resolved to hasten on through the
country, to lay his case before his excellent friend Frank Dillon, who
was the only son of a gentleman in the western parts of Galloway,
and who, he knew, was at present residing with his father. Full of the
most riotous glee, and nimble-witted as Mercutio, Frank, he was
aware, could be no less gravely wise as an adviser in a difficult
emergency, and he determined, in the present case, to be wholly
ruled by his opinion. Invigorated from thus having settled for himself
a definite course, he walked swiftly forward through the night, which
shone with the finest beauty of the moon. Yet what peace to the
murderer, whose red title not the fairest duellist, who has slain a
human being, can to his own conscience reduce? The cold glittering
leaves on the trees, struck with a quick, momentary gust, made him
start as he passed; and the shadowy foot and figure of the lover,
coming round from the back window of the lone cottage, was to his
startled apprehension the avenger of blood at hand. As he looked
afar along the glittering road, the black fir trees upon the edge of the
moor seemed men coming running down to meet him; and the long
howl of some houseless cur, and the distant hoof of the traveller,
which struck his listening ear with two or three beatings, seemed all
in the track of pursuit and vengeance.
Morning came, and to the weary fugitive was agreeably cloudy; but
the sun rose upon him in the forenoon, shining from between the
glassy, glistering clouds with far greater heat than it does from a pure
blue sky. Hawkins had now crossed many a broad acre of the weary
moorlands, fatigued and thirsty, his heart beating in his ears, and not
a drop of water that he could see to sprinkle the dry pulses of his
bosom, when he came to a long morass, which barred his
straightforward path. His first business was to quench his thirst from
a dull stank, overgrown with paddowpipe, and black with myriads of
tadpoles. There, finding himself so faint from fatigue that he could
not brook the idea of going round by the end of the moss, and being
far less able to make his way through the middle of it, by leaping
from hagg to hagg, he threw himself down on the sunny side of
some long reeds, and fell fast asleep.
He was waked by the screaming of lapwings, and the noise of a
neighbouring bittern, to a feeling of violent throbbing, headache, and
nausea, which were probably owing to the sun’s having beat upon
him whilst he lay asleep, aggravated by the reflection from the reeds.
He arose, but finding himself quite unable to pursue his journey,
again threw himself down on a small airy brow of land, to get what
breeze might be stirring abroad. There were several companies of
people at work digging peats in the moss, and one party now sat
down very near him to their dinner. One of them, a young woman,
had passed so near him, as to be able to guess, from his countenance,
that he was unwell; and in a few minutes, with the fine charity of
womanhood, she came to him with some food, of which, to satisfy
her kindness, rather than his own hunger, he ate a little. The air
changed in the afternoon, and streaming clouds of hail crossed over
that wild country, yet he lay still. Party after party left the moss, and
yet he was there. He made, indeed, a show of leaving the place at a
quick rate, to disappoint the fears of the people who had seen him at
noon, and who, as they again came near to gather up their
supernumerary clothes, were evidently perplexed on his account,
which they showed by looking first towards him and then at each
other. It was all he could do to get quite out of their sight beyond a
little eminence; and there, once more, he lay down in utter
prostration of mind and body.
Twilight began to darken upon the pools of that desolate place.
The wild birds were gone to their heathy nests, all save the curlew,
whose bravura was still sung over the fells, and borne far away into
the dim and silent night. At length a tall, powerful-looking man came
stepping through the moss, and as he passed near the poor youth,
asked, in slow speech, who he was. In the reaction of nature,
Hawkins was, in a moment, anxious about his situation, and replied
to him that he had fallen sick on his way, and was unable to go in
quest of a resting-place for the night. Approaching and turning
himself round to the youth as he arose, the genius of the place had
him on his back in a moment, and went off with him carelessly and
in silence over the heath. In about half an hour they came to a lonely
cottage, which the kind creature entered; and, setting the young man
down, without the least appearance of fatigue on his part, “Here,
gudewife,” said he, “is a bairn t’ye, that I hae foun’ i’ the moss: now,
let us see ye be gude to him.” Either this injunction was very
effective, or it was not at all necessary; for, had the youth been her
own son, come from a far country to see her, this hostess of the
cottage could not have treated him more kindly. From his little
conversation during the evening, her husband, like most very bulky
men, appeared to be of dull intellect; but there was a third personage
in the composition of his household, a younger brother, a very little
man,—the flower of the flock,—who made ample amends for his
senior brother’s deficiencies as a talker. A smattering of Church-
history had filled his soul with a thousand stories of persecution and
martyrdom, and, from some old history of America, he had gained a
little knowledge of Upper Canada, for which, Hawkins was during
the night repeatedly given to understand, he was once on the very
point of setting out, an abiding embryo of bold travel, which, in his
own eye, seemed to invest him with all the honours and privileges of
bona fide voyagers. His guest had a thousand questions put to him
on these interesting topics, less for his answers, it was evident, than
for an opportunity to the little man of setting forth his own
information. All this was tolerably fair; but it was truly disgusting
when the little oracle took the Bible after supper, and, in place of his
elder brother, who was otherwise also the head of the family,
performed the religious services of the evening, presuming to add a
comment to the chapter which he read; to enforce which, his elbow
was drawn back to the sharpest angle of edification, from which, ever
and anon unslinging itself like a shifting rhomboid, it forced forward
the stiff information in many a pompous instalment. The
pertinacious forefinger was at work too; and before it trembled the
mystic Babylon, which, in a side argument, that digit was uplifted to
denounce. Moreover, the whole lecture was given in a squeaking,
pragmatic voice, which sounded like the sharping of thatchers’
knives.
Next morning the duellist renewed his journey, hoping against
eveningtide to reach Dillon’s house, which he guessed could not now
be more than forty miles distant. About mid-afternoon, as he was
going through a small hamlet of five or six cottages, he stepped into
one of them, and requested a little water to drink. There was a
hushed solemnity, he could see in a moment, throughout the little
apartment into which, rather too unceremoniously, he had entered;
and a kind-looking matron, in a dark robe, whispered in his ear, as
she gave him a porringer of sweet water, with a little oatmeal
sprinkled upon it, that an only daughter of the house, a fine young
woman, was lying “a corpse.” Without noticing his presence, and
indeed with her face hid, sat the mother doubtless of the maiden,
heedless of the whispered consolations of two or three officious
matrons, and racking in that full and intense sorrow with which
strangers cannot intermeddle. The sloping beams of the declining
sun shone beautifully in through a small lattice, illumining a half-
decayed nosegay of flowers which stood on the sunny whitewashed
sill—emblem of a more sorrowful decay!—and after traversing the
middle of the apartment, with a thin deep bar of light, peopled by a
maze of dancing motes, struck into the white bed, where lay
something covered up and awfully indistinct, like sanctified thing not
to be gazed at, which the fugitive’s fascinated eye yet tried to shape
into the elegant body of the maiden, as she lay before her virgin
sheets purer than they, with the salt above her still and unvexed
bosom. The restricted din of boys at play—for that buoyant age is yet
truly reverential, and feels most deeply the solemn occasion of death
—was heard faint and aloof from the house of mourning. This, and
the lonely chirrup of a single sparrow from the thatch; the soft
purring of the cat at the sunny pane; the muffled tread of the
mourners over the threshold; and the audible grief of that poor
mother, seemed, instead of interruption, rather parts of the solemn
stillness.
As Hawkins was going out, after lingering a minute in this sacred
interior, he met, in the narrow passage which led to the door, a man
with the coffin, on the lid of which he read, as it was pushed up to his
very face, “Emily Robson, aged 22.” The heart of the murderer—the
seducer—was in a moment as if steeped in the benumbing waters of
petrifaction; he was horrified; he would fain have passed, but could
not for want of room; and as the coffin was not to be withdrawn in
accommodation to him, he was pushed again into the interior of the
cottage to encounter a look of piercing recognition from Emily’s
afflicted mother, who had started up on hearing the hollow grating of
the coffin as it struck occasionally on the walls of the narrow
entrance. “Take him away—take him away—take him away!” she
screamed, when she saw Hawkins, and pressed her face down on the
white bed of death. As for the youth, who was fearfully conscious of
another bloody woe which had not yet reached her heart, and of
which he was still the author, and who saw, moreover, that this poor
mother was now come to poverty, probably from his own first injury
against the peace of her family, he needed not to be told to depart.
With conscience, that truest conducting-rod, flashing its moral
electricities of shame and fear, and with knees knocking against each
other, he stumbled out of the house, and making his way by chance
to an idle quarry, overgrown with weeds, he there threw himself
down, with his face on the ground. In this situation he lay the whole
night and all next forenoon; and in the afternoon—for he had
occasionally risen to look for the assembling of the funeral train—he
joined the small group who carried his Emily to the churchyard, and
saw her young body laid in the grave. Oh! who can cast away
carelessly, like a useless thing, the finely-moulded clay, perfumed
with the lingering beauty of warm motions, sweet graces, and young
charities! But had not the young man, think ye, tenfold reason to
weep for her whom he now saw laid down within the dark shadow of
the grave?
In the evening, he found his way to Frank Dillon’s; met his friend
by chance at a little distance from his father’s house, and told him at
once his unhappy situation. “My father,” replied Frank, “cannot be
an adviser here, because he is a Justice of the Peace. But he has been
at London for some time, and I do not expect him home till to-
morrow; so you can go with me to our house for this night, where we
shall deliberate what next must be done in this truly sad affair of
yours. Come on.”
It is unnecessary for us to explain at length the circumstances
which frustrated the friendly intentions of Dillon, and which enabled
the officers of justice to trace Hawkins to his place of concealment.
They arrived that very evening; and, notwithstanding the efforts of
Frank to save his friend, secured the unhappy duellist, who, within
two days afterwards, found himself in Edinburgh, securely lodged in
jail.
The issue of Hawkins’ trial was that he was condemned to death as
a murderer. This severe sentence of the law was, however, commuted
into that of banishment for seven years. But he never again returned
to his native country. And it must be told of him also, that no
happiness ever shone upon this after-life of his. Independent of his
first crime, which brought a beautiful young woman prematurely to
the grave, he had broken rashly “into the bloody house of life,” and,
in the language of Holy Writ, “slain a young man to his hurt.”
Oh! for that still and quiet conscience—those third heavens within
a man—wherein he can soar within himself and be at peace, where
the image of God shines down, never dislimned nor long hid by those
wild racks and deep continents of gloom which come over the soul of
the blood-guilty man!
THE HEADSTONE.

By Professor Wilson.

The coffin was let down to the bottom of the grave, the planks were
removed from the heaped-up brink, the first rattling clods had struck
their knell, the quick shovelling was over, and the long, broad,
skilfully cut pieces of turf were aptly joined together, and trimly laid
by the beating spade, so that the newest mound in the churchyard
was scarcely distinguishable from those that were grown over by the
undisturbed grass and daisies of a luxuriant spring. The burial was
soon over; and the party, with one consenting motion, having
uncovered their heads in decent reverence of the place and occasion,
were beginning to separate, and about to leave the churchyard. Here
some acquaintances, from distant parts of the parish, who had not
had an opportunity of addressing each other in the house that had
belonged to the deceased, nor in the course of the few hundred yards
that the little procession had to move over from his bed to his grave,
were shaking hands, quietly but cheerfully, and inquiring after the
welfare of each other’s families. There, a small knot of neighbours
were speaking, without exaggeration, of the respectable character
which the deceased had borne, and mentioning to one another little
incidents of his life, some of them so remote as to be known only to
the grayheaded persons of the group; while a few yards farther
removed from the spot, were standing together parties who
discussed ordinary concerns, altogether unconnected with the
funeral, such as the state of the markets, the promise of the season,
or change of tenants; but still with a sobriety of manner and voice
that was insensibly produced by the influence of the simple
ceremony now closed, by the quiet graves around, and the shadow of
the spire and gray walls of the house of God.
Two men yet stood together at the head of the grave, with
countenances of sincere but unimpassioned grief. They were
brothers, the only sons of him who had been buried. And there was
something in their situation that naturally kept the eyes of many
directed upon them for a longer time, and more intently, than would
have been the case had there been nothing more observable about
them than the common symptoms of a common sorrow. But these
two brothers, who were now standing at the head of their father’s
grave, had for some years been totally estranged from each other,
and the only words that had passed between them, during all that
time, had been uttered within a few days past, during the necessary
preparations for the old man’s funeral.
No deep and deadly quarrel was between these brothers, and
neither of them could distinctly tell the cause of this unnatural
estrangement. Perhaps dim jealousies of their father’s favour—selfish
thoughts that will sometimes force themselves into poor men’s
hearts respecting temporal expectations—unaccommodating
manners on both sides—taunting words that mean little when
uttered, but which rankle and fester in remembrance—imagined
opposition of interests, that, duly considered, would have been found
one and the same—these, and many other causes, slight when single,
but strong when rising up together in one baneful band, had
gradually but fatally infected their hearts, till at last they who in
youth had been seldom separate, and truly attached, now met at
market, and, miserable to say, at church, with dark and averted faces,
like different clansmen during a feud.
Surely if anything could have softened their hearts towards each
other, it must have been to stand silently, side by side, while the
earth, stones, and clods, were falling down upon their father’s coffin.
And, doubtless, their hearts were so softened. But pride, though it
cannot prevent the holy affections of nature from being felt, may
prevent them from being shown; and these two brothers stood there
together, determined not to let each other know the mutual
tenderness that, in spite of them, was gushing up in their hearts, and
teaching them the unconfessed folly and wickedness of their
causeless quarrel.
A headstone had been prepared, and a person came forward to
plant it. The elder brother directed him how to place it—a plain
stone, with a sand-glass, skull, and cross-bones, chiselled not rudely,
and a few words inscribed. The younger brother regarded the
operation with a troubled eye, and said, loudly enough to be heard by
several of the bystanders, “William, this was not kind in you;—you
should have told me of this. I loved my father as well as you could
love him. You were the elder, and, it may be, the favourite son; but I
had a right in nature to have joined you in ordering this headstone,
had I not?”
During these words, the stone was sinking into the earth, and
many persons who were on their way from the grave returned. For a
while the elder brother said nothing, for he had a consciousness in
his heart that he ought to have consulted his father’s son in designing
this last becoming mark of affection and respect to his memory; so
the stone was planted in silence, and now stood erect, decently and
simply among the other unostentatious memorials of the humble
dead.
The inscription merely gave the name and age of the deceased, and
told that the stone had been erected “by his affectionate sons.” The
sight of these words seemed to soften the displeasure of the angry
man, and he said, somewhat more mildly, “Yes, we were his
affectionate sons, and since my name is on the stone, I am satisfied,
brother. We have not drawn together kindly of late years, and
perhaps never may; but I acknowledge and respect your worth; and
here, before our own friends, and before the friends of our father,
with my foot above his head, I express my willingness to be on better
and other terms with you, and if we cannot command love in our
hearts, let us, at least, brother, bar out all unkindness.”
The minister, who had attended the funeral, and had something
intrusted to him to say publicly before he left the churchyard, now
came forward, and asked the elder brother why he spake not
regarding this matter. He saw that there was something of a cold and
sullen pride rising up in his heart—for not easily may any man hope
to dismiss from the chamber of his heart even the vilest guest, if once
cherished there. With a solemn and almost severe air, he looked
upon the relenting man, and then, changing his countenance into
serenity, said gently,—
Behold how good a thing it is,
And how becoming well,
Together such as brethren are
In unity to dwell.

The time, the place, and this beautiful expression of a natural


sentiment, quite overcame a heart in which many kind, if not warm,
affections dwelt; and the man thus appealed to bowed down his head
and wept.
“Give me your hand, brother;” and it was given, while a murmur of
satisfaction arose from all present, and all hearts felt kindlier and
more humanely towards each other.
As the brothers stood fervently, but composedly, grasping each
other’s hands, in the little hollow that lay between the grave of their
mother, long since dead, and that of their father, whose shroud was
haply not yet still from the fall of dust to dust, the minister stood
beside them with a pleasant countenance, and said, “I must fulfil the
promise I made to your father on his deathbed. I must read to you a
few words which his hand wrote at an hour when his tongue denied
its office. I must not say that you did your duty to your old father; for
did he not often beseech you, apart from one another, to be
reconciled, for your own sakes as Christians, for his sake, and for the
sake of the mother who bare you, and, Stephen, who died that you
might be born? When the palsy struck him for the last time, you were
both absent, nor was it your fault that you were not beside the old
man when he died. As long as sense continued with him here, did he
think of you two, and of you two alone. Tears were in his eyes; I saw
them there, and on his cheek too, when no breath came from his lips.
But of this no more. He died with this paper in his hand; and he
made me know that I was to read it to you over his grave. I now obey
him:
“‘My sons, if you will let my bones lie quiet in the grave, near the
dust of your mother, depart not from my burial till, in the name of
God and Christ, you promise to love one another as you used to do.
Dear boys, receive my blessing.’”
Some turned their heads away to hide the tears that needed not to
be hidden; and when the brothers had released each other from a
long and sobbing embrace, many went up to them, and in a single
word or two expressed their joy at this perfect reconcilement. The
brothers themselves walked away from the churchyard, arm in arm
with the minister, to the manse. On the following Sabbath they were
seen sitting with their families in the same pew; and it was observed
that they read together off the same Bible when the minister gave out
the text, and that they sang together, taking hold of the same psalm-
book. The same psalm was sung (given out at their own request), of
which one verse had been repeated at their father’s grave; and a
larger sum than usual was on that Sabbath found in the plate for the
poor, for Love and Charity are sisters. And ever after, both during the
peace and the troubles of this life, the hearts of the brothers were as
one, and in nothing were they divided.
THE WIDOW’S PREDICTION:
A TALE OF THE SIEGE OF NAMUR.

On the morning of the 30th August 1695, just as the sun began to
tinge the dark and blood-stained battlements of Namur, a
detachment of Mackay’s Scottish regiment made their rounds,
relieving the last night-sentinels, and placing those of the morning.
As soon as the party returned to their quarters, and relaxed from the
formalities of military discipline, their leader, a tall, muscular man,
of about middle age, with a keen eye and manly features, though
swarthy and embrowned with toil, and wearing an expression but
little akin to the gentle or the amiable, moved to an angle of the
bastion, and, leaning on his spontoon, fixed an anxious gaze on the
rising sun. While he remained in this position, he was approached by
another officer, who, slapping him roughly on the shoulder, accosted
him in these words—
“What, Monteith! are you in a musing mood? Pray, let me have the
benefit of your morning meditations.”
“Sir!” said Monteith, turning hastily round. “Oh! ’tis you, Keppel.
What think you of this morning?”
“Why, that it will be a glorious day for some; and for you and me, I
hope, among others. Do you know that the Elector of Bavaria
purposes a general assault to-day?”
“I might guess as much, from the preparations going on. Well,
would it were to-morrow!”
“Sure you are not afraid, Monteith?”
“Afraid! It is not worth while to quarrel at present; but methinks
you, Keppel, might have spared that word. There are not many men
who might utter it and live.”
“Nay, I meant no offence; yet permit me to say, that your words
and manner are strangely at variance with your usual bearing on a
battle-morn.”
“Perhaps so,” replied Monteith; “and, but that your English
prejudices will refuse assent, it might be accounted for. That sun will
rise to-morrow with equal power and splendour, gilding this earth’s
murky vapours, but I shall not behold his glory.”
“Now, do tell me some soothful narrative of a second-sighted seer,”
said Keppel. “I promise to do my best to believe it. At any rate, I will
not laugh outright, I assure you.”
“I fear not that. It is no matter to excite mirth; and, in truth, I feel
at present strangely inclined to be communicative. Besides, I have a
request to make; and I may as well do something to induce you to
grant it.”
“That I readily will, if in my power,” replied Keppel. “So, proceed
with your story, if you please.”
“Listen attentively, then—and be at once my first and my last
confidant.
“Shortly after the battle of Bothwell Bridge, I joined the troop
commanded by Irvine of Bonshaw; and gloriously did we scour the
country, hunting the rebel Covenanters, and acting our pleasure
upon man, woman, and child, person and property. I was then but
young, and, for a time, rather witnessed than acted in the wild and
exciting commission which we so amply discharged. But use is all in
all. Ere half-a-dozen years had sped their round, I was one of the
prettiest men in the troop at everything. It was in the autumn of
1684, as I too well remember, that we were engaged in beating up the
haunts of the Covenanters on the skirts of Galloway and Ayrshire. A
deep mist, which covered the moors thick as a shroud—friendly at
times to the Whigs, but, in the present instance, their foe—concealed
our approach, till we were close upon a numerous conventicle. We
hailed, and bade them stand; but, trusting to their mosses and glens,
they scattered and fled. We pursued in various directions, pressing
hard upon the fugitives. In spite of several morasses which I had to
skirt, and difficult glens to thread, being well mounted, I gained
rapidly on a young mountaineer, who, finding escape by flight
impossible, bent his course to a house at a short distance, as hoping
for shelter there, like a hare to her form. I shouted to him to stand;
he ran on. Again I hailed him; but he heeded not; when, dreading to
lose all trace of him, should he gain the house, I fired. The bullet took
effect. He fell, and his heart’s blood gushed on his father’s threshold.
Just at that instant an aged woman, alarmed by the gallop of my
horse, and the report of the pistol, rushed to the door, and
stumbling, fell upon the body of her dying son. She raised his
drooping head upon her knee, kissed his bloody brow, and screamed
aloud, ‘Oh, God of the widow and the fatherless, have mercy on me!’
One ghastly convulsive shudder shook all her nerves, and the next
moment they were calm as the steel of my sword; then raising her
pale and shrivelled countenance, every feature of which was fixed in
the calm, unearthly earnestness of utter despair, or perfect
resignation, she addressed me, every word falling distinct and
piercing on my ear like dropping musketry.
“‘And hast thou this day made me a widowed, childless mother?
Hast thou shed the precious blood of this young servant of Jehovah?
And canst thou hope that thy lot will be one of unmingled happiness?
Go, red-handed persecutor! Follow thine evil way! But hear one
message of truth from a feeble and unworthy tongue. Remorse, like a
bloodhound, shall dog thy steps; and the serpent of an evil
conscience shall coil around thy heart. From this hour thou shalt
never know peace. Thou shalt seek death, and long to meet it as a
friend; but it shall flee thee. And when thou shalt begin to love life,
and dread death, then shall thine enemy come upon thee; and thou
shalt not escape. Hence to thy bloody comrades, thou second Cain!
Thou accursed and banished from the face of Heaven and of mercy!

“‘Foul hag!’ I exclaimed, it would take little to make me send thee
to join thy psalm-singing offspring!’
“‘Well do I know that thou wouldst if thou wert permitted!’ replied
she. ‘But go thy way, and bethink thee how thou wilt answer to thy
Creator for this morning’s work!’
“And, ceasing to regard me, she stooped her head over the dead
body of her son. I could endure no more, but wheeled around, and
galloped off to join my companions.
“From that hour, I felt myself a doomed and miserable man. In
vain did I attempt to banish from my mind the deed I had done, and
the words I had heard. In the midst of mirth and revelry, the dying
groan of the youth, and the words of doom spoken by his mother,
rung for ever in my ears, converting the festal board to a scene of
carnage and horror, till the very wine-cup seemed to foam over with
hot bubbling gore. Once I tried—laugh, if you will—I tried to pray;
but the clotted locks of the dying man, and the earnest gaze of the
soul-stricken mother, came betwixt me and Heaven,—my lip faltered
—my breath stopped—my very soul stood still, for I knew that my
victims were in Paradise, and how could I think of happiness—I,
their murderer—in one common home with them? Despair took
possession of my whole being. I rushed voluntarily to the centre of
every deadly peril, in hopes to find an end to my misery. Yourself can
bear me witness that I have ever been the first to meet, the last to
retire from, danger. Often, when I heard the battle-signal given, and
when I passed the trench, or stormed the breach, in front of my
troop, it was less to gain applause and promotion than to provoke the
encounter of death. ’Twas all in vain. I was doomed not to die, while I
longed for death. And now—”
“Well, by your own account, you run no manner of risk, and at the
same time are proceeding on a rapid career of military success,” said
Keppel; “and, for my life, I cannot see why that should affect you,
supposing it all perfectly true.”
“Because you have not yet heard the whole. But listen a few
minutes longer. During last winter, our division, as you know, was
quartered in Brussels, and was very kindly entertained by the
wealthy and good-natured Flemings. Utterly tired of the heartless
dissipation of life in a camp, I endeavoured to make myself agreeable
to my landlord, that I might obtain a more intimate admission into
his family circle. To this I was the more incited, that I expected some
pleasure in the society of his daughter. In all I succeeded to my wish.
I became quite a favourite with the old man, and procured ready
access to the company of his child. But I was sufficiently piqued to
find, that in spite of all my gallantry, I could not learn whether I had
made any impression upon the heart of the laughing Fanchon. What
peace and playful toying could not accomplish, war and sorrow did.
We were called out of winter quarters, to commence what was
anticipated to be a bloody campaign. I obtained an interview to take
a long and doubtful farewell. In my arms the weeping girl owned her
love, and pledged her hand, should I survive to return once more to
Brussels. Keppel, I am a doomed man; and my doom is about to be
accomplished! Formerly I wished to die; but death fled me. Now I
wish to live; and death will come upon me! I know I shall never more
see Brussels, nor my lovely little Fleming. Wilt thou carry her my last
farewell; and tell her to forget a man who was unworthy of her love—
whose destiny drove him to love, and be beloved, that he might
experience the worst of human wretchedness? You’ll do this for me,
Keppel?”
“If I myself survive, I will. But this is some delusion—some strong
dream. I trust it will not unnerve your arm in the moment of the
storm.”
“No! I may die—must die; but it shall be in front of my troop, or in
the middle of the breach. Yet how I long to escape this doom! I have
won enough of glory; I despise pillage and wealth; but I feel my very
heartstrings shrink from the now terrible idea of final dissolution.
Oh! that the fatal hour were past, or that I had still my former
eagerness to die! Keppel, if I dared, I would to-day own myself a
coward.”
“Come with me,” said Keppel, “to my quarters. The night air has
made you aguish. The cold fit will yield to a cup of as generous Rhine
wine as ever was drunk on the banks of the Sambre.” Monteith
consented, and the two moved off to partake of the stimulating and
substantial comforts of a soldier’s breakfast in the Netherlands.
It was between one and two in the afternoon. An unusual stillness
reigned in the lines of the besiegers. The garrison remained equally
silent, as watching in deep suspense on what point the storm
portended by this terrible calm would burst. A single piece of
artillery was discharged. Instantly a body of grenadiers rushed from
the intrenchments, struggled over masses of ruins, and mounted the
breach. The shock was dreadful. Man strove with man, and blow
succeeded to blow, with fierce and breathless energy. The English
reached the summit, but were almost immediately beaten back,
leaving numbers of their bravest grovelling among the blackened
fragments. Their leader, Lord Cutts, had himself received a
dangerous wound in the head; but disregarding it, he selected two
hundred men from Mackay’s regiment, and putting them under the
command of Lieutenants Cockle and Monteith, sent them to restore
the fortunes of the assault. Their charge was irresistible. Led on by
Monteith, who displayed a wild and frantic desperation, rather than
bravery, they broke through all impediments, drove the French from
the covered way, seized on one of the batteries, and turned the
cannon against the enemy. To enable them to maintain this
advantage, they were reinforced by parties from other divisions.
Keppel, advancing in one of those parties, discovered the mangled
form of his friend Monteith, lying on heaps of the enemy on the very
summit of the captured battery. He attempted to raise the seemingly
lifeless body. Monteith opened his eyes,—“Save me!” he cried; “save
me! I will not die! I dare not—I must not die!”
It were too horrid to specify the ghastly nature of the mortal
wounds which had torn and disfigured his frame. To live was
impossible. Yet Keppel strove to render him some assistance, were it
but to soothe his parting spirit. Again he opened his glazing eyes,—“I
will resist thee to the last!” he cried, in a raving delirium. “I killed
him but in the discharge of my duty. What worse was I than others?
Poor consolation now! The doom—the doom! I cannot—dare not—
must not—will not die!” And while the vain words were gurgling in
his throat, his head sunk back on the body of a slaughtered foe, and
his unwilling spirit forsook his shattered body.—Edinburgh Literary
Journal.
THE LADY OF WARISTOUN.

The estate of Waristoun, near Edinburgh, now partly covered by


the extended streets of the metropolis on its northern side, is
remarkable in local history for having belonged to a gentleman, who
in the year 1600, was cruelly murdered at the instigation of his wife.
This unfortunate lady, whose name was Jean Livingstone, was
descended from a respectable ancestry, being the daughter of
Livingstone, the laird of Dunipace, in Stirlingshire, and at an early
age was married to John Kincaid, the laird of Waristoun, who, it is
believed, was considerably more advanced in years than herself. It is
probable that this disparity of age laid the foundation of much
domestic strife, and led to the tragical event now to be noticed. The
ill-fated marriage and its results form the subject of an old Scottish
ballad, in which the proximate cause of the murder is said to have
been a quarrel at the dinner-table:
It was at dinner as they sat,
And when they drank the wine,
How happy were the laird and lady
Of bonny Waristoun!

But he has spoken a word in jest;


Her answer was not good;
And he has thrown a plate at her,
Made her mouth gush with blude.

Whether owing to such a circumstance as is here alluded to, or a bite


which the laird is said to have inflicted upon her arm, is immaterial;
the lady, who appeared to have been unable to restrain her
malignant passions, conceived the diabolical design of having her
husband assassinated. There was something extraordinary in the
deliberation with which this wretched woman approached the awful
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookgate.com

You might also like