by Doug Lowe
Author Of Networking
All-In-One Desk Reference For Dummies
The fun and easy way to Create dynamic applications you can really use!
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,
California, where the motto is, “We almost beat USC!” with his wife, the
youngest of his three daughters, and a couple of outrageously cute puppies.
He is the Information Technology Director for Blair, Church & Flynn, an
engineering firm in nearby Clovis, CA, and he is also one of those obsessivecompulsive
decorating nuts who used to put up tens of thousands of lights at
Christmas until his wife saw the electric bill, so now he creates energy-efficient
computer-controlled Halloween decorations that rival Disney’s Haunted Mansion.
Author’s Acknowledgments
I’d like to thank everyone involved with making this book a reality, especially
project editor Paul Levesque, who did a great job following through on all the
little editorial details needed to put a book of this scope together on time,
and didn’t panic when the “on time” part of that equation was in question.
Thanks also to Ken Cox, who gave the manuscript a thorough review and
offered many excellent suggestions for improvements, and to copy editor
Barry Childs-Helton, who crossed all the i’s and dotted all the t’s, or something
like that, and in the process somehow turned my strange prose into
readable English. And, as always, thanks to all the behind-the-scenes people
who chipped in with help I’m not even aware of.
Contents at a Glance
Introduction ....................1
Part I: Introducing ASP.NET 2.0
Application Development...........7
Chapter 1: Designing ASP.NET 2.0 Applications .............9
Chapter 2: Using Visual Studio 2005 .....................33
Part II: Building Secure Applications ..............53
Chapter 3: Designing Secure ASP.NET Applications .............55
Chapter 4: Building a User Authentication Application .............79
Part III: Building E-Commerce Applications........105
Chapter 5: Building a Product Catalog Application ..............107
Chapter 6: Building a Shopping Cart Application .................143
Part IV: Building Back-End Applications .........221
Chapter 7: Building a Product Maintenance Application.............223
Chapter 8: Building a Report Application ..................269
Part V: Building Community Applications.........287
Chapter 9: Building a Content Management System.............289
Chapter 10: Building a Web Forum..................329
Chapter 11: Building a Blog Application..........375
Part VI: The Part of Tens ................423
Chapter 12: Ten New Features of ASP.NET 2.0...................425
Chapter 13: Ten Rookie Mistakes ......................445
Chapter 14: Ten Database Design Tips ...............451
Appendix: About the CD ..............455
Index ....................459
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
Chapter 1: Designing ASP.NET 2.0 Applications
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
Designing Relational Databases ...................................................................18
Step 1: Create a charter for the database..........................................18
Step 2: Make a list and check it twice ................................................19
Step 3: Add keys ...................................................................................21
Step 4: Normalize the database ..........................................................23
Step 5: Denormalize the database......................................................27
Step 6: Pick legal SQL names...............................................................28
Step 7: Draw a picture..........................................................................28
Designing Objects ..........................................................................................29
Diagramming Classes with UML...................................................................29
Drawing classes ....................................................................................31
Drawing arrows.....................................................................................32
Chapter 2: Using Visual Studio 2005
Creating a Basic Hello World Application ...................................................34
Creating a new Web site ......................................................................35
Adding a label control..........................................................................37
Running the application ......................................................................39
Adding a Code-Behind File............................................................................40
Adding a Text Box and a Button...................................................................42
Working with Folders and Other Project Items..........................................46
Debugging Web Applications........................................................................47
Creating a calculator page...................................................................47
Working in Break mode........................................................................49
Displaying data values .........................................................................50
Controlling execution and setting breakpoints ................................51
Deploying ASP.NET Applications .................................................................52
Part II: Building Secure Applications
Chapter 3: Designing Secure ASP.NET Applications
Understanding ASP.NET User Authentication ............................................55
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
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
Building the Login page .......................................................................95
Building the Register page ..................................................................97
Building the Recover Password page ................................................98
Building the Password Confirmation page......................................100
Building the Change Password page................................................101
Building the User List page ...............................................................102
Part III: Building E-Commerce Applications
Chapter 5: Building a Product Catalog Application
The Application’s User Interface................................................................107
The Product List page........................................................................108
The Product Detail page ....................................................................109
The Cart page......................................................................................111
Designing the Product Catalog Application..............................................111
Designing the Product Database ................................................................113
The Categories table ..........................................................................113
The Products table.............................................................................114
The FeaturedProducts table .............................................................115
Creating the database ........................................................................116
Adding some test data .......................................................................118
Querying the database ......................................................................119
Connecting to the database ..............................................................121
The Application’s Folders ...........................................................................122
Building the Master Page ............................................................................122
Building the Product List Page ...................................................................123
The Default.aspx file ..........................................................................124
The code-behind file for the Default.aspx page (C# version).......132
The code-behind file for the Default.aspx page
(Visual Basic version) ....................................................................134
Building the Product Detail page ...............................................................136
The Product.aspx file .........................................................................136
The code-behind file for the Product.aspx page (C# version) .....140
The code-behind file for the Product.aspx page
(Visual Basic version) ....................................................................141
Building the Cart Page.................................................................................142
Chapter 6: Building a Shopping Cart Application
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
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
Modifying the Product Detail Page ............................................................168
Building the Cart Page.................................................................................171
The Cart.aspx file ...............................................................................171
The code-behind file for the Cart page............................................173
Building the Check Out Page ......................................................................181
The CheckOut.aspx file......................................................................181
The code-behind file for the Check Out page .................................189
Creating the Customer Class ......................................................................194
Creating the ShoppingCart Class ...............................................................199
Creating the CartItem Class ........................................................................203
Creating the Order Class .............................................................................207
Creating the OrderDB Class........................................................................212
Part IV: Building Back-End Applications
Chapter 7: Building a Product Maintenance Application
The Application’s User Interface................................................................224
The Menu page ...................................................................................224
The Category Maintenance page......................................................224
The Product Maintenance page........................................................227
Designing the Application...........................................................................229
Designing the Database ...............................................................................231
The Categories table ..........................................................................231
The Products table.............................................................................232
Creating the database ........................................................................233
Adding some test data .......................................................................234
SQL statements for the Categories table.........................................235
SQL statements for the Products table ...........................................236
Connecting to the database ..............................................................237
The Application’s Folders ...........................................................................238
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
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
Chapter 9: Building a Content Management System
Making Some Basic Decisions ....................................................................289
The Content Management System’s User Interface.................................291
The Login page ...................................................................................292
The Home page...................................................................................292
The Department Home page.............................................................293
The Content List page........................................................................294
The Content Detail page ....................................................................295
Designing the Database ...............................................................................297
The Departments table......................................................................297
The ContentTypes table ....................................................................298
The ContentItems table .....................................................................298
Creating the Database .................................................................................299
Adding Test Data ..........................................................................................301
SQL statements for working with the database .............................302
Connecting to the database ..............................................................303
Creating the User Accounts ........................................................................303
Building the Master Page ............................................................................304
Building the Login Page...............................................................................307
Building the Home Page ..............................................................................308
Building the Department Home Page ........................................................309
Building the Content List Page ...................................................................313
The List.aspx file ................................................................................314
The code-behind file for the Content List page..............................317
Building the Content Detail Page ...............................................................318
The Detail.aspx file.............................................................................319
The code-behind file for the Content Detail page ..........................325
Chapter 10: Building a Web Forum
Designing the Forum Application...............................................................329
The User Interface for the Forum Application .........................................331
The Forum Home page ......................................................................331
The Threads page...............................................................................332
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
Designing the Blog Application ..................................................................375
Designing the User Interface.......................................................................376
The Blog Home page ..........................................................................376
The Blog page .....................................................................................378
The Comments page ..........................................................................379
The Leave Comment page.................................................................379
The Login page ...................................................................................380
The Register page...............................................................................381
The My Blogs page .............................................................................381
The New Post page.............................................................................383
Designing the Database ...............................................................................383
The Blogs table ...................................................................................384
The Posts table ...................................................................................384
The Comments table..........................................................................385
Creating the Database .................................................................................386
Adding test data .................................................................................388
SQL statements for working with the database .............................389
Connecting to the database ..............................................................390
Building the Master Page ............................................................................390
Building the Blog Home Page .....................................................................392
Building the Blog Page.................................................................................395
The Blog.aspx page ............................................................................395
The code-behind file for the Blog page............................................399
Building the Comments Page......................................................................402
The Comments.aspx page .................................................................402
The code-behind file for the Comments page.................................405
Building the Leave Comment Page ............................................................407
The Comment.aspx page ...................................................................407
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
Chapter 12: Ten New Features of ASP.NET 2.0
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
The Wizard Control......................................................................................440
The Generics Feature...................................................................................441
The Web Site Administration Tool .............................................................443
Chapter 13: Ten Rookie Mistakes
Coding Too Soon ..........................................................................................445
Skimping On Documentation......................................................................446
Inadequate Testing.......................................................................................447
Abusing State Features................................................................................447
Not Validating Input Data ............................................................................448
Reinventing the Wheel.................................................................................448
Not Asking for Help......................................................................................449
Chapter 14: Ten Database Design Tips
Use the Right Number of Tables.................................................................451
Avoid Repeating Data ..................................................................................452
Avoid Redundant Data.................................................................................452
Use a Naming Convention...........................................................................452
Avoid nulls ....................................................................................................453
Avoid Secret Codes ......................................................................................453
Use Constraints Wisely................................................................................454
Use Triggers When Appropriate.................................................................454
Use Stored Procedures ................................................................................454
Appendix: About the CD
System Requirements..................................................................................455
Using the CD .................................................................................................456
Using the Source Files .................................................................................456
Troubleshooting...........................................................................................457
Index........................................................................459
Introduction
Welcome to ASP.NET 2.0 Everyday Apps For Dummies, the book that
teaches ASP.NET 2.0 Web programming by example. In this book,
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 provides
you with actual working code to build the most popular types of applications
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 maintenance 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 comprehensive
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 examples.
Designed using the easy-to-follow For Dummies format, this book helps
you get the information you need without laboring to find it.
Part I: Introducing ASP.NET 2.0
Application Development
Chapter 1 in this part is a general introduction to ASP.NET application development.
It explains bedrock concepts such as the system-development life
cycle, using layered architectures in ASP.NET applications, and designing
relational databases. Then Chapter 2 presents a quick introduction to building
ASP.NET applications using Visual Studio 2005 or Visual Web Developer
2005 Express Edition.
Part II: Building Secure Applications
This part shows you how to build security into your applications. Chapter 3
is an introduction to ASP.NET 2.0 security. Then, behold: Chapter 4 presents
the first application in this book — a user-authentication application that
you can incorporate into any application that requires users to register and log in.
Part III: Building E-Commerce
Applications
This part provides two complete e-commerce applications. The first is an
online product catalog that lets users view your products via a Web site. The
second is a shopping-cart application that lets users purchase products. In
fact, the shopping-cart application is an extension of the product-catalog
application: It lets users purchase products they’ve had a chance to view via the online catalog.
Part IV: Building Back-End Applications
Just about all public Web applications have a back end that isn’t visible to the
general public. This part presents two applications you’ll often need in the
back end. The first is a file-maintenance application that lets you add, update,
or delete records in a products database. And the second is a report application
that generates reports based on data gathered from Web visitors.
Introduction 3
Part V: Building Community Applications
The three applications in this part let users create Web-based communities.
Chapter 9 presents a content-management system for users’ Web sites;
Chapter 10 presents a forum application for posting messages and replies.
And Chapter 11 presents a blog application that lets users create blog articles
that visitors can then read and comment upon.
Part VI: The Part of Tens
No For Dummies book would be complete without a Part of Tens. In Chapter
12, you get an overview of the most important new features of ASP.NET 2.0. If
you’re an experienced ASP.NET programmer but haven’t worked with version
2.0 yet, you may want to read this chapter first to see what new features have
been added for this version.
Next Chapter 13 describes ten-or-so rookie mistakes that (with any luck) you
can avoid. And finally, Chapter 14 presents ten-or-so tips for designing your databases.