Copyright © 2003 by Microsoft Corporation PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2003 by Microsoft Corporation All rights reserved No part of the contents of this book may be reproduced form or by any means without the written permission of the publisher Library of Congress Cataloging-in-Publication Data pending Printed and bound in the United States of America 4 5 6 7 8 9 QWT Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors wo information about international editions, contact your local Microsoft Corpo contact Microsoft Press International directly at fax (425) 936-7329 Visit o www.microsoft.com/mspress Send comments to tkinput@microsoft.com ActiveX, BackOffice, FrontPage, IntelliSense, JScript, Microsoft, Microsoft P Visual C#, Visual J#, Visual Studio, Win32, Windows, and Windows NT are trademarks or trademarks of Microsoft Corporation in the United States an Other product and company names mentioned herein may be the tradema owners The example companies, organizations, products, domain names, e-mail a people, places, and events depicted herein are fictitious No association wit company, organization, product, domain name, e-mail address, logo, perso event is intended or should be inferred Acquisitions Editor: Kathy Harding Project Editor: Aileen Wrothwell Technical Editor: Jack Beaudry Copy Editor: Jennifer Harris Game Development: Michel Pahud Body Part No X09-46540 About This Book Welcome to MCAD/MCSD Training Kit—Developing Web Applications with Microsoft Visual Basic NET and Microsoft Visual C# NET By completing the lessons and exercises in this book, you will acquire the skills and knowledge necessary to develop Web-based applications in Microsoft Visual Basic NET or Microsoft Visual C# This book is designed for developers interested in developing Web applications at an intermediate and advanced level It includes developing server-based applications that use the NET Framework to present HTML content and retrieve data from client browsers running over the Internet This book also addresses the objectives of the Microsoft Certified Professional Exam 70-305 and Exam 70-315 Programming is a conceptual skill that requires hands-on practice as well as familiarity with many facts This training kit provides that practice in the form of labs at the end of each chapter In order to be prepared for the Microsoft Certified Professional Exam 70-305 or 70-315 you must complete these labs In some cases this will require use of additional materials – in particular the Visual Studio online Help NOTEFor more information about becoming a Microsoft Certified Application Developer (MCAD) or a Microsoft Certified Solution Developer (MCSD), see the section titled “The Microsoft Certified Professional Program” later in this introduction The “Getting Started” section of this introduction provides important setup instructions that describe the hardware and software requirements to complete the exercises in this course It also provides information about the networking configuration necessary to complete some of the hands-on exercises Read through this section thoroughly before you start the lessons Intended Audience This book was created for software developers who need to design, plan, implement and support Web applications or who plan to take the related Microsoft Certified Professional exams: Developing and Implementing Web Applications with Microsoft Visual Basic NET and Microsoft Visual Studio NET (Exam 70305) Developing and Implementing Web Applications with Microsoft Visual C# NET and Microsoft Visual Studio NET (Exam 70-315) Prerequisites This course requires that students meet the prerequisites that follow Be able to create Windows applications using Microsoft Visual Studio NET in either the Visual Basic NET or Visual C# NET programming language Have a basic understanding of the object-oriented programming concepts including classes, properties, methods, and events Understand the fundamental elements in the Hypertext Markup Language (HTML) used to author Web content Understand how Web content is stored and accessed over the Internet This includes being able to explain the roles of Web servers, Internet protocols, and Web clients (such as browsers) Reference Materials You might find the following reference materials useful: The Visual Studio NET online Help The World Wide Web Consortium Web site (http://www.w3c.org) The Microsoft ASP.NET public newsgroup (news://msnews.microsoft.com/ microsoft.public.dotnet.framework.aspnet) About the CD-ROM The Supplemental Course Materials CD-ROM contains a variety of informational aids that may be used throughout this book: eBook A complete electronic version (eBook) of this training kit Lesson samples Most lessons in this training kit include sample Web forms and other files that demonstrate the code shown in the training kit These samples are organized into two Visual Studio solutions named MCSDWebAppsVB, for the Visual Basic NET samples, and MCSDWebAppsCS, for the Visual C# samples Completed labs Each chapter in this training kit concludes with a lab containing a series of exercises that reinforce the skills you learned Completed versions of these applications are included in the chapter folders of the MCSDWebAppsVB and MCSDWebAppsCS solutions so that you can compare your results You can also use these completed applications as a reference if you get stuck while completing an exercise Required files Practice files, such as sample databases, that are required to perform the hands-on procedures You should use these files when indicated in the exercises Sample exam questions To practice taking a certification exam, you can use the sample exam installed by the CD-ROM The sample questions help you assess your understanding of the materials presented in this book About the DVD The DVD contains a 60-day evaluation edition of Microsoft Visual Studio NET Professional CAUTION The 60-day Evaluation Edition provided with this training is not the full retail product and is provided only for the purposes of training and evaluation Microsoft Technical Support does not support this evaluation edition For additional support information regarding this book and the CD-ROM and DVD (including answers to commonly asked questions about installation and use), visit the Microsoft Press Technical Support Web site at http://www.microsoft.com/mspress/support/ You can also email tkinput@microsoft.com, or send a letter to Microsoft Press, Attn: Microsoft Press Technical Support, One Microsoft Way, Redmond, WA 98502-6399 Features of This Book Each chapter contains sections that are designed to help you get the most educational value from the chapter Each chapter opens with a “Before You Begin” section, which prepares you for completing the chapter The chapters are then divided into lessons Each lesson contains the reference and procedural information used for a specific skill The “Summary” section identifies the key concepts from the lesson The “Lab” section provides hands-on exercises that reinforce each of the skills taught in each of the chapter lessons The exercises give you an opportunity to use the skills being presented or explore the part of the application being described Wherever possible, the exercises in a lab build on each other to create a complete application by the end of that lab The exercises offer step-by-step procedures that are identified with a bullet symbol like the one to the left of this paragraph At the end of each chapter is the “Review” section that you can use to test what you have learned The appendix, “Questions and Answers,” contains all of the questions asked in each chapter review and the corresponding answers Notes Several types of Notes appear throughout the lessons Notes marked Tip contain explanations of possible results or alternative methods for performing tasks Notes marked Important contain information that is essential to completing a task Notes marked Note contain supplemental information Notes marked Caution contain warnings about possible loss of data Notational Conventions The following notational conventions are used throughout this book Characters or commands that you type appear in bold type Bold type is also used for glossary terms the first time they are defined in text Italic in syntax statements indicates placeholders for variable information Italic is also used for book titles, elements from the NET Framework, and programming language keywords Names of files and folders appear in initial capital letters, except when you are to type them directly Unless otherwise indicated, you can use lowercase letters when you type a file name in a dialog box or at a command prompt Filename extensions, when they appear without a file name, are in lowercase letters Acronyms appear in all uppercase letters Monospace type represents code samples, examples of screen text, or entries that you might type at a command prompt or in initialization files Icons represent specific sections in the book as follows: Icon Represents Supplemental course materials You will find these materials on the Supplemental Course Materials CD- ROM A hands-on exercise You should perform the exercise to give yourself an opportunity to use the skills being presented in the lesson Chapter and Appendix Overview This self-paced training kit combines notes, hands-on procedures, and review questions to teach you how to create Web applications with Visual Studio NET It is designed to be completed from beginning to end, but you can choose a customized track and complete only the sections that interest you (See the next section, “Finding the Best Starting Point for You,” for more information.) If you choose the customized track option, see the “Before You Begin” section in each chapter Any hands-on procedures that require preliminary work from preceding chapters refer to the appropriate chapters The book is divided into the following sections and chapters: The “About This Book” section contains an overview and introduces the components of this training kit Read this section thoroughly to get the greatest educational value from this training kit and to plan which lessons you will complete Chapter 1, “Introduction to Web Programming,” introduces you to the concepts and terms used throughout this book, including how Web applications work, the parts of a Web application, how the NET Framework is organized, and how to use the Visual Studio programming environment The sections in this chapter are intended to provide a high-level overview of concepts that are explained in greater detail in subsequent chapters Chapter 2, “Creating Web Forms Applications,” teaches how to create a Web application using Visual Studio NET and ASP.NET You learn how to start a new project, create a basic user interface, write code to respond to events, and preserve data within your application Because ASP.NET is integrated with Microsoft Internet Information Services (IIS), you also learn about IIS and how to use it to organize your Web application Chapter 3, “Working with Web Objects,” explains creating and organizing objects in Visual Studio NET, using the objects provided by ASP.NET, and saving objects and data between requests to Web forms Visual Basic, Visual C#, and ASP.NET are based on a common framework, so the object-oriented programming techniques you learn in this chapter apply through all aspects of NET programming Chapter 4, “Creating a User Interface,” demonstrates how to use ASP.NET controls to create a user interface for a multi-page Web forms application This chapter teaches you about different styles of Web forms, how to choose controls based on the tasks you want to perform, how to validate data fields, and how to navigate between the Web forms in your application Chapter 5, “Storing and Retrieving Data with ADO.NET,” shows how to use ADO.NET to access and modify data stored in SQL, OLE, and Oracle databases from Web forms You learn about the data tools included with Visual Studio and how to use them to create connections to, get records from, and perform commands on databases You will also learn how to display data on a Web form using either data binding or code Finally, you learn how to use transactions to maintain the integrity of a database Chapter 6, “Catching and Correcting Errors,” explains how to deal with problems that may occur in your application due to external circumstances This chapter helps you identify, anticipate, and handle these types of problems effectively within your application Chapter 7, “Advanced Web Forms Programming,” teaches how to perform advanced programming tasks that don’t fit neatly into any other category Although you can successfully create Web applications without knowing the topics covered in this chapter, the lessons found here are an important part of mainstream Web programming and they help complete your skill set Chapter 8, “Maintaining Security,” covers how to control access to your Web application using the three different techniques that ASP.NET provides for identifying and authorizing users You also learn how to Insert an object element in the Help topic using the HTML Help ActiveX control For example, the following object element adds the “numbers” index keyword: