by Doug Lowe
ASP.NET 2.0
Everyday Apps
FOR
DUMmIES
‰
01_597760 ffirs.qxp 1/11/06 9:48 PM Page iii
by Doug Lowe
ASP.NET 2.0
Everyday Apps
FOR
DUMmIES
‰
01_597760 ffirs.qxp 1/11/06 9:48 PM Page iii
01_597760 ffirs.qxp 1/11/06 9:48 PM Page ii
ASP.NET 2.0
Everyday Apps
FOR
DUMmIES
‰
01_597760 ffirs.qxp 1/11/06 9:48 PM Page i
01_597760 ffirs.qxp 1/11/06 9:48 PM Page ii
by Doug Lowe
ASP.NET 2.0
Everyday Apps
FOR
DUMmIES
‰
01_597760 ffirs.qxp 1/11/06 9:48 PM Page iii
TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A
COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR-
THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR-
MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE.
FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE
CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other 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 technical support, please visit
www.wiley.com/techsupport
.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2005937349
ISBN-13: 978-0-7645-9776-3
ISBN-10: 0-7645-9776-0
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1O/QS/QS/QW/IN
01_597760 ffirs.qxp 1/11/06 9:48 PM Page iv
About the Author
Doug Lowe has written a whole bunch of computer books, including more
than 35 For Dummies books, including the Java All-in-One Desk Reference
For Dummies, Networking For Dummies, 7th Edition, the Networking All-in-One
Desk Reference For Dummies, PowerPoint 2003 For Dummies, and the Word
2003 All-in-One Desk Reference For Dummies. He lives in sunny Fresno,
Acquisitions, Editorial, and
Media Development
Senior Project Editor: Paul Levesque
Acquisitions Editor: Katie Feltman
Copy Editor: Barry Childs-Helton
Technical Editor: Ken Cox
Editorial Manager: Leah Cameron
Media Development Manager:
Laura VanWinkle
Media Development Supervisor:
Richard Graves
Editorial Assistant: Amanda Foxworth
Cartoons: Rich Tennant (
www.the5thwave.com
)
Composition Services
Project Coordinator: Adrienne Martinez
Layout and Graphics: Carl Byers, Andrea Dahl,
Lauren Goddard, Denny Hager,
Joyce Haughey, Barbara Moore,
Lynsey Osborn, Heather Ryan
Proofreaders: Laura Albert, Leeann Harney,
Jessica Kramer, TECHBOOKS Production
Services
Indexer: TECHBOOKS Production Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Appendix: About the CD ............................................455
Index .......................................................................459
02_597760 ftoc.qxp 1/11/06 9:49 PM Page ix
02_597760 ftoc.qxp 1/11/06 9:49 PM Page x
Table of Contents
Introduction..................................................................1
About This Book...............................................................................................1
Conventions Used in This Book .....................................................................1
How This Book Is Organized...........................................................................2
Part I: Introducing ASP.NET 2.0 Application Development ...............3
Part II: Building Secure Applications ...................................................3
Part III: Building E-Commerce Applications........................................3
Part IV: Building Back-End Applications..............................................3
Part V: Building Community Applications...........................................4
Part VI: The Part of Tens ......................................................................4
About the CD...........................................................................................4
Icons Used in This Book..................................................................................4
Where to Go from Here....................................................................................5
Part I: Introducing ASP.NET 2.0
Application Development ...............................................7
Chapter 1: Designing ASP.NET 2.0 Applications . . . . . . . . . . . . . . . . . . .9
The Development Treadmill .........................................................................10
Building Models..............................................................................................11
What Is an Application Design?....................................................................12
Using Layered Architectures ........................................................................12
How many layers? ................................................................................13
Model-View-Controller .........................................................................15
Designing the user interface ...............................................................16
Designing the Business Rules Layer ..................................................17
Designing the Data Access Layer .......................................................17
Examining three types of authentication ..........................................56
Configuring forms-based authentication...........................................57
Configuring authorization ...................................................................57
Understanding membership providers .............................................59
Using ASP.NET Login Controls......................................................................60
Using the Login control .......................................................................60
Using the CreateUserWizard control .................................................62
Using the PasswordRecovery control ...............................................66
Using the ChangePassword control ...................................................69
Using the LoginView control ...............................................................72
Using the LoginName control .............................................................73
Using the LoginStatus control.............................................................74
Protecting Against Other Threats ................................................................75
Avoid malicious scripts .......................................................................75
Preventing SQL-injection attacks .......................................................77
Hiding error messages.........................................................................78
Chapter 4: Building a User Authentication Application . . . . . . . . . . . .79
The Application’s User Interface..................................................................80
Designing the User Authorization Application...........................................88
The Application’s Folders .............................................................................88
The web.config Files ......................................................................................89
Building Pages for the User Authentication Application ..........................91
Building the Master Page.....................................................................91
Building the Content page...................................................................93
Building the Admin page .....................................................................94
ASP.NET 2.0 Everyday Apps For Dummies
xii
02_597760 ftoc.qxp 1/11/06 9:49 PM Page xii
Building the Login page .......................................................................95
Building the Register page ..................................................................97
(Visual Basic version) ....................................................................141
Building the Cart Page .................................................................................142
Chapter 6: Building a Shopping Cart Application . . . . . . . . . . . . . . . .143
Considering Some Basic Decisions ............................................................144
The User Interface........................................................................................145
The Product List page........................................................................146
The Product Detail page....................................................................146
The Cart page......................................................................................146
The Check Out page...........................................................................148
The Completed page ..........................................................................152
xiii
Table of Contents
02_597760 ftoc.qxp 1/11/06 9:49 PM Page xiii
Designing the Cart Database ......................................................................153
The Customers table..........................................................................154
The Orders table.................................................................................155
The OrderItems table.........................................................................156
Creating the database........................................................................157
Adding some test data .......................................................................159
Querying the database ......................................................................159
Inserting order data into the database............................................159
Connecting to the database ..............................................................161
The Application’s Folders ...........................................................................161
Designing the Classes ..................................................................................162
The Customer class............................................................................162
The ShoppingCart class.....................................................................163
The CartItem class .............................................................................164
The Order class ..................................................................................164
The OrderDB class .............................................................................165
Building the Master page ............................................................................165
02_597760 ftoc.qxp 1/11/06 9:49 PM Page xiv
Building the Master Page ............................................................................238
Building the Menu Page ..............................................................................239
Building the Category Maintenance Page .................................................240
The CatMaint.aspx file .......................................................................240
The code-behind file for the Catalog Maintenance page...............245
Building the Product Maintenance Page...................................................249
The ProdMaint.aspx file.....................................................................249
The code-behind file for the Product Maintenance page..............265
Chapter 8: Building a Report Application . . . . . . . . . . . . . . . . . . . . . . .269
The Application’s User Interface................................................................270
Designing the Application...........................................................................271
Building the Database..................................................................................272
Designing the database......................................................................272
Creating the database........................................................................274
Adding test data .................................................................................274
SQL statements to retrieve the order data .....................................275
Connecting to the database ..............................................................276
Building the Master Page ............................................................................276
Building the Order User Control ................................................................277
Building the Default Page ............................................................................283
The Default.aspx file ..........................................................................283
The code-behind file for the default page .......................................284
Building the Print Order page.....................................................................286
Part V: Building Community Applications ...................287
Chapter 9: Building a Content Management System . . . . . . . . . . . . .289
Making Some Basic Decisions ....................................................................289
The Content Management System’s User Interface.................................291
The Login page ...................................................................................292
The Home page...................................................................................292
The Messages page ............................................................................332
The New Thread page........................................................................334
The Post Reply page ..........................................................................334
Designing the Database ...............................................................................335
The Forums table ...............................................................................335
The Topics table .................................................................................336
The Threads table ..............................................................................337
The Messages table............................................................................338
Creating the Database .................................................................................338
Adding Test Data ..........................................................................................340
SQL statements for working with the database .............................341
Connecting to the database ..............................................................342
Building the Master Page ............................................................................342
Building the Forum Home Page..................................................................343
The Default.aspx page .......................................................................344
The code-behind file for the Forum Home page.............................346
Building the Threads Page..........................................................................350
The Threads.aspx page......................................................................350
The code-behind file for the Threads page.....................................353
Building the Messages Page........................................................................355
The Messages.aspx page ...................................................................355
The code-behind file for the Messages page ..................................359
Building the New Thread Page ...................................................................360
The NewThread.aspx page................................................................361
The code-behind file for the New Thread page ..............................363
Building the New Message Page.................................................................367
The NewMessage.aspx page .............................................................368
The code-behind file for the New Message page............................371
Chapter 11: Building a Blog Application . . . . . . . . . . . . . . . . . . . . . . . .375
Designing the Blog Application ..................................................................375
The code-behind file for the Leave Comment page .......................409
Building the Login Page...............................................................................410
Building the Register Page ..........................................................................411
Building the My Blogs Page ........................................................................412
The MyBlogs.aspx page.....................................................................413
The code-behind file for the My Blogs page ...................................416
Building the New Post Page ........................................................................418
The NewPost.aspx page.....................................................................418
The code-behind file for the New Post page...................................421
Part VI: The Part of Tens............................................423
Chapter 12: Ten New Features of ASP.NET 2.0 . . . . . . . . . . . . . . . . . . .425
The New Code-Behind Model .....................................................................426
App_ Folders.................................................................................................427
Master Pages.................................................................................................428
Creating a Master Page......................................................................429
Completing a Master Page.................................................................430
Creating a Content page ....................................................................431
New Data Controls .......................................................................................432
The SqlDataSource control ...............................................................433
The GridView control.........................................................................436
The DetailsView control ....................................................................438
The FormView Control.......................................................................439
Login Controls ..............................................................................................440
xvii
Table of Contents
02_597760 ftoc.qxp 1/11/06 9:49 PM Page xvii
The Wizard Control......................................................................................440
The Generics Feature...................................................................................441
The Web Site Administration Tool .............................................................443
Chapter 13: Ten Rookie Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445
you’ll find eight complete ASP.NET applications. We’re not talking trivial
Hello-World-type applications here. Instead, they’re real-world applications
like shopping carts and discussion forums. You can use any of them as-is, or
modify them as you see fit. So you’ve got workable stuff already included.
(What a concept.)
About This Book
This book is a practical introduction to ASP.NET 2.0 Web programming. It pro-
vides you with actual working code to build the most popular types of appli-
cations on the Web. These applications enable you to:
ߜ Restrict access to registered users, for all or part of your Web site
ߜ Sell products online via your Web site
ߜ Provide back-end functions for your public Web site, such as file mainte-
nance and reporting
ߜ Let users manage specific types of online content
ߜ Create discussion forums and blogs
ASP.NET 2.0 Everyday Apps For Dummies doesn’t pretend to be a comprehen-
sive reference for every detail of ASP.NET programming. Instead, it takes a
learn-by-example approach, under the assumption that you are already a
pretty competent programmer who can best learn by seeing real-world exam-
ples. Designed using the easy-to-follow For Dummies format, this book helps
you get the information you need without laboring to find it.
Conventions Used in This Book
This book has a lot of code in it. You’ll find complete listing of every line of
code, both C# and Visual Basic, for each of the eight applications presented
in this book. You’ll also find listings for supporting files such as SQL scripts
to generate databases and web.config files that provide configuration infor-
mation for the applications.
03_597760 intro.qxp 1/11/06 9:49 PM Page 1
Most of these listings include reference numbers that correspond to num-
bered explanations in the text. In most cases, these explanations apply to
2
ASP.NET 2.0 Everyday Apps For Dummies
03_597760 intro.qxp 1/11/06 9:49 PM Page 2