- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
ASP.NET
DATABASE PROGRAMMING
WEEKENDEND CRASHCRASH COURSECOURSE™
JASON |
15 |
BUTLER |
|
TONY |
|
CAUDILL |
|
|
HOURS |
|
30 Sessions That |
CD-ROM |
Will Have You |
with assessment |
Creating Dynamic, |
software, sample |
Data-Driven Web |
code, and more |
Applications in |
|
Only 15 Hours |
ASP.NET
Database Programming
Weekend Crash Course™
Jason Butler and Tony Caudill
Cleveland, OH • Indianapolis, IN • New York, NY
ASP.NET Database Programming Weekend Crash Course™
Published by
Hungry Minds, Inc.
909 Third Avenue New York, NY 10022 www.hungryminds.com
Copyright © 2002 Hungry Minds, Inc. All rights reserved. No part of this book, including interior design, cover design, and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written permission of the publisher. Library of Congress Catalog Card No.: 2001089343 ISBN: 0-7645-4830-1
Printed in the United States of America 10 9 8 7 6 5 4 3 2 1
IB/SZ/RR/QR/IN
Distributed in the United States by Hungry Minds, Inc.
Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by International Thomson Publishing for Germany, Austria, and Switzerland; by Distribuidora Cuspide for Argentina; by LR International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS
Computer Publishing Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V. for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland.
For general information on Hungry Minds’ products and services please contact our Customer Care department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002. For sales inquiries and reseller information, including discounts, premium and bulk quantity sales, and foreign-language translations, please contact our Customer Care department at 800-434-3422, fax 317-572-4002 or write to Hungry Minds, Inc., Attn: Customer Care Department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256.
For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care department at 212-884-5000.
For information on using Hungry Minds’ products and services in the classroom or for ordering examination copies, please contact our Educational Sales department at 800-434-2086 or fax 317-572-4005. For press review copies, author interviews, or other publicity information, please contact our Public Relations department at 317-572-3168 or fax 317-572-4168.
For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
Trademarks: Hungry Minds, the Hungry Minds logo, Weekend Crash Course and related trademarks are trademarks or registered trademarks of Hungry Minds, Inc. in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. Hungry Minds, Inc., is not associated with any product or vendor mentioned in this book.
is a trademark of Hungry Minds, Inc.
About the Authors
Jason Butler is a Principal Consultant with PricewaterhouseCoopers LLP. Jason has built numerous Microsoft-centric Web applications for Fortune 500 companies. When not writing code, he religiously works out at a gym near his home in northern Virginia. Jason is also a devoted Hootie & The Blowfish fan.
Tony Caudill is a Principal Consultant at PricewaterhouseCoopers LLP. Tony has written and deployed custom Microsoft Solutions for twenty Fortune 500 Companies to support the integration of SAP, Siebel, and other ERP/CRM applications. When not managing system implementation projects, he avidly pursues surfing in southern California at his favorite beaches and tackles skiing at Big Bear.
Dedications
I would like to dedicate this book to my family and friends who have provided me with tremendous support and happiness throughout my life. To my mother, Marian, for inspiring me to be that best person that I possibly can. To Donna, without whose support, encouragement, and patience this book would never have been completed. To my father, Al, for always providing much needed advice and support. And to my stepfather, Steve, for being who you didn't have to be. I would also like to send my prayers to all of the families impacted by the tragic events of September 11, 2001.
- Jason
I would like to dedicate this book to my family and friends, who have supported me and given me tremendous joy over the past year: Marie and Bradley Caudill. I also would like to offer my prayers for the families impacted by the tremendous tragedies of September 11 and my support for the policemen, firefighters, and communities of New York and Washington. And to the US military leadership, such as my father, Sy, and his wife Sue, who like so many soldiers before them will face a long, challenging, and difficult road to building a foundation for us all in our search for peace.
-Tony
Credits
Acquisitions Editor
Sharon Cox
Project Editor
Sharon Nash
Development Editor
Michael Koch
Technical Editors
Todd Meister
Peter MacIntyre
Copy Editor
Maarten Reilingh
Editorial Assistant
Cordelia Heaney
Editorial Manager
Mary Beth Wakefield
Project Coordinator
Maridee Ennis
Graphics and Production Specialists
Sean Decker
Joyce Haughey
Gabriele McCann
Kristin McMullan
Jill Piscitelli
Betty Schulte
Erin Zeltner
Quality Control Technician
John Bitter
Susan Moritz
Angel Perez
Carl Pierce
Sossity R. Smith
Proofreading and Indexing
Senior Vice President, Technical Publishing TECHBOOKS Production Services
Richard Swadley
Vice President and Publisher
Joseph B. Wikert
Preface
ASP.NET Database Programming Weekend Crash Course™ introduces the reader to ASP.NET database programming in one weekend: 30 sessions of a half hour each, for 15 hours stretching from Friday afternoon to Sunday evening. At the end of each sec-
tion of the book, you’ll get a chance to test your knowledge before continuing. Good luck!
Who Should Read This Book
This book is for people who want to learn to write ASP.NET applications in order to access and manipulate data in a Web environment. This book assumes that you have a basic understanding of Web development, including experience with Visual Basic or Visual Basic Scripting in developing ASP-based applications. The book’s focus is on ASP.NET and ADO.NET as a suite of components used in data-driven Web application development. It includes a CD-ROM with ASP.NET editor software and sample code mentioned in the text.
Organization and Presentation
We’ve organized the book into 30 sessions, each requiring approximately 30 minutes. We divide the sessions as follows:
Friday evening. Sessions 1 through 4. Reading time: 2 hours.
Saturday morning. Sessions 5 through 10. Reading time: 3 hours
Saturday afternoon. Sessions 11 through 16. Reading time: 3 hours.
Saturday evening. Sessions 17 through 20. Reading time: 2 hours.
Sunday morning. Sessions 21 through 26. Reading time: 3 hours.
Sunday afternoon. Sessions 27 through 30. Reading time: 2 hours.
At the end of each session, we present questions designed to check your progress. The text is sprinkled with icons designed to catch your attention.
vi |
Foreword |
Tip
Note
CD-ROM
The “minutes to go” icons mark your progress in the session.
The Tip icons offer suggestions on style and mention shortcuts that can save programming effort.
The Note icons highlight incidental or technical information that clarifies and expands upon the discussion.
The CD-ROM icon refers to material furnished on the book’s CD. Use it to find electronic versions of programs and software elements mentioned in the text.
Contacting the Authors
We can’t guarantee we will solve all of your ASP.NET database programming problems in this book, but we promise to take a look at your questions and see if we can help. If you get stuck, you can contact us at the following e-mail address:
tony_and_jason@hotmail.com
Acknowledgments
T ony: First, I would like to acknowledge my wife, Marie, who has been extremely patient and has provided tremendous support throughout the course of this project. A lot of new life changes and accomplishments are hitting us this year, including our first child, Brad, and of course the publication of this book. I’d like to thank Jason Butler, my co-author, for making this opportunity available and being a solid source of support in its development.
J ason: I would like to dedicate this book to two people: my mother, who has always inspired me to be the best person I can, and Donna, without whose patience, support, and encouragement this book would never have been completed.
There are also a few people whom I would like to thank for their support throughout this project. First, I would like to thank the PwC-eArmyU team, especially Chrystyna, Chris, FJ, Mark, Volodya, PV, Julie, Travis, and Michael. They truly are some of the finest people with whom I have had the privilege to work. I would also like to thank my fathers, Steve and Al, for their unwavering support.
Last but not least, I would like to thank Tony Caudill, my co-author, for being a great friend and mentor.
Tony and Jason: We would both like to thank PricewaterhouseCoopers LLP and Hungry Minds for providing the financial and motivational support to accomplish this task while supporting an extensive consulting practice.
Health and Peace . . .
Contents at a Glance |
|
Preface.................................................................................................................................... |
v |
Introduction ........................................................................................................................ |
xvii |
FRIDAY ...................................................................................................................... |
2 |
Part I—Friday Evening .................................................................................................. |
4 |
Session 1–Introducing ASP.NET .................................................................................................. |
5 |
Session 2–Setting Up .NET ...................................................................................................... |
13 |
Session 3–Designing a Database ............................................................................................... |
21 |
Session 4–Building a Database ................................................................................................. |
29 |
SATURDAY ................................................................................................................. |
44 |
Part II—Saturday Morning ..................................................................................................... |
46 |
Session 5–Using SQL: A Primer ................................................................................................. |
47 |
Session 6–XML: A Primer ......................................................................................................... |
55 |
Session 7–Developing ASP.NET Pages ........................................................................................ |
61 |
Session 8–Using HTML Controls ................................................................................................ |
69 |
Session 9–Using Web Controls .................................................................................................. |
79 |
Session 10–Introducing User Controls ....................................................................................... |
89 |
Part III—Saturday Afternoon ............................................................................................... |
100 |
Session 11–Validating User Input ........................................................................................... |
101 |
Session 12–Maintaining State in ASP.NET ................................................................................ |
113 |
Session 13–Authentication and Authorization ......................................................................... |
123 |
Session 14–ASP.NET Caching .................................................................................................. |
135 |
Session 15–Introducing ADO.NET ............................................................................................ |
149 |
Session 16–Navigating the ADO.NET Object Model ..................................................................... |
155 |
Part IV—Saturday Evening ................................................................................................... |
168 |
Session 17–Opening a Connection .......................................................................................... |
169 |
Session 18–Executing Commands ............................................................................................ |
177 |
Session 19–Using DataReaders ................................................................................................ |
187 |
Session 20–Introducing DataSets, Part I .................................................................................. |
197 |
SUNDAY .................................................................................................................. |
208 |
Part V—Sunday Morning ...................................................................................................... |
210 |
Session 21–Introducing DataSets, Part II ................................................................................. |
211 |
Session 22–Introducing Data Binding ...................................................................................... |
223 |
Session 23–Using the DataGrid Control with Bound Data ........................................................... |
233 |
Session 24–Beating the CRUD out of the DataGrid Control ......................................................... |
243 |
Session 25–Data Shaping with ADO.NET .................................................................................. |
255 |
Session 26–Handling ADO.NET Errors ....................................................................................... |
263 |
Part VI—Sunday Afternoon .................................................................................................. |
276 |
Session 27–SOAP It Up! ......................................................................................................... |
277 |
Session 28–Web Services ....................................................................................................... |
283 |
Session 29–Migrating from ASP to ASP.Net .............................................................................. |
291 |
Session 30–Migrating from ADO to ADO.NET ............................................................................. |
299 |
Appendix A–Answers to Part Reviews .................................................................................... |
309 |
Appendix B–What’s on the CD-ROM ....................................................................................... |
315 |
Appendix C–ADO.NET Class Descriptions ................................................................................ |
319 |
Appendix D–Coding Differences in ASP and ASP.NET ............................................................... |
355 |
Index .................................................................................................................................. |
369 |
Hungry Minds, Inc. End User License Agreement ..................................................................... |
387 |