TEAM LinG Professional VB 2005 01_575368 ffirs.qxd 10/7/05 10:46 PM Page i 01_575368 ffirs.qxd 10/7/05 10:46 PM Page ii Professional VB 2005 Bill Evjen, Billy Hollis, Rockford Lhotka, Tim McCarthy, Rama Ramachandran, Kent Sharkey, Bill Sheldon 01_575368 ffirs.qxd 10/7/05 10:46 PM Page iii Professional VB 2005 Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-7645-7536-5 ISBN-10: 0-7645-7536-8 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/SW/RQ/QV/IN Library of Congress Cataloging-in-Publication Data: Professional Visual Basic 2005 / Bill Evjen [et al.]. p. cm. Includes index. ISBN-13: 978-0-7645-7536-5 (paper/website) ISBN-10: 0-7645-7536-8 (paper/website) 1. Microsoft Visual BASIC. 2. BASIC (Computer program language) 3. Microsoft .NET. I. Evjen, Bill. QA76.73.B3P7485 2005 005.2’768 — dc22 2005012585 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESEN- TATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WAR- RANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUIT- ABLE 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 FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION 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 United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Visual Basic is a registered trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_575368 ffirs.qxd 10/7/05 10:46 PM Page iv About the Authors Bill Evjen is an active proponent of .NET technologies and community-based learning initiatives for .NET. He has been actively involved with .NET since the first bits were released in 2000. In the same year, Bill founded the St. Louis .NET User Group ( www.stlnet.org), one of the world’s first .NET user groups. Bill is also the founder and the executive director of the International .NET Association (INETA – www.ineta.org), which represents more than 375,000 members worldwide. Based in St. Louis, Missouri, Bill is an acclaimed author and speaker on ASP.NET and XML Web Services. He has written or coauthored more than 10 books, including Professional C# 2005 and Professional ASP.NET 2.0 (Wrox), XML Web Services for ASP.NET, ASP.NET Professional Secrets (Wiley), and more. Bill is a technical director for Reuters, the international news and financial services company, and he travels the world speaking to major financial institutions about the future of the IT industry. He gradu- ated from Western Washington University in Bellingham, Washington, with a Russian language degree. When he isn’t tinkering on the computer, he can usually be found at his summer house in Toivakka, Finland. You can reach Bill at evjen@yahoo.com. To Kalle – Welcome to the family! Billy Hollis is coauthor of the first book ever published on Visual Basic .NET, VB.NET Programming on the Public Beta (Wrox Press) as well as numerous other books and articles on .NET. Billy is a Microsoft regional director and an MVP, and he was selected as one of the original .NET “Software Legends.” He writes a monthly column for MSDN Online and is heavily involved in training, consultation, and soft- ware development on the Microsoft .NET platform, focusing on smart-client development and commer- cial packages. He frequently speaks at industry conferences such as Microsoft’s Professional Developer Conference, TechEd, and COMDES. Billy is a member of the INETA speakers’ bureau and speaks at user group meetings all over the United States. Rockford Lhotka is the principal technology evangelist for Magenic Technologies ( www.magenic.com), a company focused on delivering business value through applied technology and one of the nation’s premiere Microsoft Gold Certified Partners. Rockford is the author of several books, including Expert Visual Basic .NET and C# Business Objects. He is a Microsoft Software Legend, regional director, MVP, and INETA speaker. He is a columnist for MSDN Online and contributing author for Visual Studio Magazine, and he regularly presents at major conferences around the world — including Microsoft PDC, Tech Ed, VS Live! and VS Connections. For more information go to www.lhotka.net. For my Mom and Dad, whose love and guidance have been invaluable in my life. Thank you! Tim McCarthy is a principal engineer at InterKnowlogy, where he architects and builds highly scalable n-tier web and smart-client applications utilizing the latest Microsoft platforms and technologies. Tim’s expertise covers a wide range of Microsoft technologies, including, but not limited to: .NET Framework (ASP.NET/Smart Clients/Web Services), Active Directory, UDDI, SQL Server, Windows SharePoint Services/SharePoint Portal Server 2003, and Service Oriented Architecture (SOA) applications. Tim has worked as a project technical lead/member as well as in a technical consulting role for several Fortune 500 companies. He has held the Microsoft Certified Solution Developer (MCSD) and Microsoft Certified Trainer (MCT) certifications for several years and was one of the first wave of developers to earn the Microsoft Certified Application Developer (MCAD) for .NET and MCSD for .NET certifications. He also holds the Microsoft Certified Database Administrator certification for SQL Server 2000. 01_575368 ffirs.qxd 10/7/05 10:46 PM Page v Tim has been an author and technical reviewer for several books from Wrox Press and most recently was a lead author on Professional VB.NET 2003. His other books include Professional Commerce Server 2000, and Professional ADO 2.5 Programming. Tim is currently working as a lead author on the next edition of Professional VB.NET. Tim has written numerous articles for the Developer .NET Update newsletter, devel- oped packaged presentations for MSDN, and has written a whitepaper for Microsoft on using COM+ services in .NET. He has also written articles for SQL Server Magazine and Windows & .NET Magazine. Tim has spoken at technical conferences around the world and several San Diego area user groups (includ- ing both .NET and SQL Server groups) and he has been a regular speaker at the Microsoft Developer Days conference in San Diego for the last several years. Tim has also delivered MSDN webcasts, many of which were repeat requests from Microsoft. Tim also teaches custom .NET classes to companies in need of expert .NET mentoring and training. Tim holds a B.B.A. in marketing from the Illinois Institute of Technology as well as an M.B.A. in market- ing from National University. Before becoming an application developer, Tim was an officer in the United States Marine Corps. Tim’s passion for .NET is only surpassed by his passion for Notre Dame athletics. I dedicate this book to everybody in my family who supports me. Jasmine, some day you will be writing books, too! Rama Ramachandran is a software architect at DKR Capital, a major hedge fund company in Stamford, Connecticut. He is a Microsoft Certified Solutions Developer and Site-Builder and has excelled in designing and developing WinForms and Web applications using .NET, ASP.NET, Visual Basic and SQL Server. Rama has more than 15 years’ experience with all facets of the software development lifecycle and has cowritten Introducing .NET, Professional ASP Data Access, Professional Visual InterDev Programming (all Wrox Press), and four books on classic Visual Basic. Rama is also the “ASP Pro” at Devx.com, where he maintains ASP-related columns. He teaches .NET Development and Web Development for Fairfield University’s master’s degree in software engineering, and at the University of Connecticut. You can reach Rama at ramabeena@hotmail.com. This book is dedicated to my wife, Beena, and our children, Ashish and Amit. They make my life whole. I’m great at writing about technology but get tongue-tied trying to say how much I love and care about the three of you. I am grateful to our prayer-answering God for your laughing, mischievous, adoring lives. Thanks for being there, Beens. I love you. Kent Sharkey. Born in an igloo and raised by wolves in a strange realm called “Manitoba,” Kent Sharkey wandered the wilderness until found by a group of kind technical evangelists and migrated to Redmond. He now is content strategist (yeah, he doesn’t know what he’s supposed to do either) for ASP.NET content on MSDN. When not answering email he dreams of sleeping, complains to everyone around (come to think of it, he does that while answering email as well), and attempts to keep his house- mates (Babi, Cica, and Squirrel) happy. As with all else, to Margaret. Thank you. Bill Sheldon is a software architect and engineer originally from Baltimore, Maryland. Holding a degree in Computer Science from the Illinois Institute of Technology (IIT) and a Microsoft Certified Solution Developer (MCSD) qualification, Bill has been employed as an engineer since resigning his commission with the U.S. Navy following the first Gulf War. Bill is involved with the San Diego .NET User Group and writes for Windows and .NET magazines, including the twice monthly Developer .NET Update email newsletter. He is also a frequent online presenter for MSDN and speaks at live events such as Microsoft Developer Days. He lives with his wife, Tracie, in Southern California, where he is employed as a princi- pal engineer with InterKnowlogy. You can reach Bill at bills@interknowlogy.com. 01_575368 ffirs.qxd 10/7/05 10:46 PM Page vi Credits Acquisitions Editor Katie Mohr Development Editors Eileen Bien Calabro Ami Frank Sullivan Technical Editor Brian Patterson Production Editor Pamela Hanley Copy Editor Foxxe Editorial Services Editorial Manager Mary Beth Wakefield Vice President & Executive Group Publisher Richard Swadley Vice President and Publisher Joseph B. Wikert Project Coordinator Ryan Steffen Graphics and Production Specialists Carrie A. Foster Lauren Goddard Denny Hager Barbara Moore Lynsey Osborn Alicia South Quality Control Technicians Laura Albert John Greenough Leeann Harney Jessica Kramer Brian H. Walls Proofreading TECHBOOKS Production Services Indexing Broccoli Information Management 01_575368 ffirs.qxd 10/7/05 10:46 PM Page vii 01_575368 ffirs.qxd 10/7/05 10:46 PM Page viii Contents Introduction xxv Chapter 1: What Is Microsoft .NET? 1 What Is .NET? 1 A Broad and Deep Platform for the Future 2 What’s Wrong with DNA and COM? 2 An Overview of the .NET Framework 3 The Common Language Runtime 4 Key Design Goals 5 Metadata 7 Multiple-Language Integration and Support 7 A Common Type System 8 Namespaces 8 The Next Layer — The .NET Class Framework 8 What Is in the .NET Class Framework? 9 User and Program Interfaces 10 Windows Forms 11 Web Forms 11 Console Applications 12 Web Services 12 XML as the .NET Metalanguage 12 The Role of COM 13 No Internal Use of COM 13 Some Things Never Change . . . 13 .NET Drives Changes in Visual Basic 14 How .NET Affects You 14 A Spectrum of Programming Models 14 Reducing Barriers to Internet Development 15 Libraries of Prewritten Functionality 15 Easier Deployment 15 The Future of .NET 16 Major Differences in .NET 2.0 16 Summary 17 02_575368 ftoc.qxd 10/7/05 10:47 PM Page ix [...]... Visual Basic NET challenged traditional VB developers to learn dramatic new concepts and techniques 2005 brings us an enhanced Visual Basic language (renamed this time Visual Basic 2005) New features have been added that cement this language’s position as a true object-oriented language With Visual Basic 2005, it is still going to be a challenge for the traditional VB6 developers to learn, but it is an... basics of browser-based programming in Visual Basic are included, professional Web developers should instead refer to Professional ASP.NET 2.0 (Wrox Press) What You Need to Use This Book Although, it is possible to create Visual Basic applications using the command-line tools contained in the NET Framework SDK, you will need Visual Studio 2005 (Professional or higher), which includes the NET Framework SDK,...Contents Chapter 2: Introducing Visual Basic 2005 and Visual Studio 2005 19 Visual Studio NET — Startup Visual Studio NET 20 21 The Solution Explorer My Project References Assembly Information Screen The New Code Window The Properties Window Dynamic Help 22 23 24 25 26 29 30 Working with Visual Basic 2005 Form Properties Set in Code 31 32 Enhancing the Sample Application... written to help experienced developers learn about Visual Basic 2005 From those who are just starting the transition from earlier versions to those who have used Visual Basic for a while and need to gain a deeper understanding, this book provides a discussion on the most common programming tasks and concepts you need Professional Visual Basic 2005 offers a wide-ranging presentation of Visual Basic concepts,... My.Resources My.User My.WebServices Summary Chapter 10: Exception Handling and Debugging A Brief Review of Error Handling in VB6 Exceptions in NET Important Properties and Methods of an Exception How Exceptions Differ from the Err Object in VB6 Structured-Exception-Handling Keywords in VB. NET 287 289 291 292 293 294 295 298 299 303 307 308 308 308 309 311 312 314 314 315 315 The Try, Catch, and Finally Keywords... in these older versions In addition: ❑ Some chapters make use of SQL Server 2005 However, you can also run the example code using Microsoft’s SQL Express, which ships with Visual Studio 2005 ❑ Several chapters make use of Internet Information Services (IIS) IIS ships with Windows 2003 Server, Windows 2000 Server, Windows 2000 Professional, and Windows XP, although it is not installed by default ❑ Chapter... drawbacks by using the common language runtime (CLR) xxvi Introduction Chapter 2, “Introducing Visual Basic 2005 and Visual Studio 2005 — This chapter provides a first look at a Visual Basic application As we develop this application, you’ll take a tour of some of the new features of Visual Studio 2005 Chapter 3, “Variables and Types” — This chapter introduces many of the types commonly used in Visual... Document Object Model (DOM) 400 405 414 418 xiv Contents XSLT Transforms XSLT Transforming between XML Standards Using XML in Visual Basic 2005 Other Classes and Interfaces in System.Xml.Xsl ADO.NET ADO.NET and SQL Server 2000’s Built-In XML Features XML and SQL Server 2005 Summary Chapter 13: Security in the NET Framework 2.0 Security Concepts and Definitions Permissions in the System.Security.Permissions... import and alias existing namespaces within projects This chapter also looks at the new My namespace that was made available in Visual Basic 2005 Chapter 10, “Exception Handling and Debugging” — This chapter covers how error handling and debugging work in Visual Basic 2005 by discussing the CLR exception handler and the new Try Catch Finally structure We also look at error and trace logging, and how... reuse the Web browser control in your applications Appendix A, “The Visual Basic Compiler” — This appendix looks at the Visual Basic compiler vbc.exe and the functionality it provides Appendix B, “Visual Basic Resources” — This appendix provides a short list of VB resources that are out there for you Conventions We have used a number of different styles of text and layout in this book to help differentiate . TEAM LinG Professional VB 2005 01_575368 ffirs.qxd 10/7/05 10:46 PM Page i 01_575368 ffirs.qxd 10/7/05 10:46 PM Page ii Professional VB 2005 Bill Evjen, Billy Hollis, Rockford. on Professional VB. NET 2003. His other books include Professional Commerce Server 2000, and Professional ADO 2.5 Programming. Tim is currently working as a lead author on the next edition of Professional. written or coauthored more than 10 books, including Professional C# 2005 and Professional ASP.NET 2.0 (Wrox), XML Web Services for ASP.NET, ASP.NET Professional Secrets (Wiley), and more. Bill is