Visual FoxPro to Visual Basic .NET Les Pinter 800 East 96th Street, Indianapolis, Indiana 46240 Visual FoxPro to Visual Basic .NET Associate Publisher Copyright©2004 by Sams Publishing Michael Stephens All rights reserved. No part of this book shall be reproduced, stored in Acquisitions Editor a retrieval system, or transmitted by any means, electronic, mechani- Loretta Yates cal, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the Development Editor use of the information contained herein. Although every precaution Sean Dixon has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any Managing Editor liability assumed for damages resulting from the use of the informa- Charlotte Clapp tion contained herein. Project Editor International Standard Book Number: 0-672-32649-3 George E. Nedeff Library of Congress Catalog Card Number: 2003099326 Copy Editor Printed in the United States of America Margaret Berson First Printing: May 2004 09 08 07 4 3 Indexer Ken Johnson Trademarks Proofreader All terms mentioned in this book that are known to be trademarks or Elizabeth Scott service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this Technical Editor book should not be regarded as affecting the validity of any trademark Martìn Saliìas or service mark. Publishing Coordinator Warning and Disclaimer Cindy Teeters Every effort has been made to make this book as complete and as Multimedia Developer accurate as possible, but no warranty or fitness is implied. The infor- Dan Scherf mation provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity Book Designer with respect to any loss or damages arising from the information Gary Adair contained in this book or from the use of the CD or programs accom- panying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected] Contents at a Glance Introduction....................................................................................................1 1 Differences Between Visual FoxPro and Visual Basic .NET............................5 2 Building Simple Applications in Visual FoxPro and Visual Basic .NET.......79 3 Building a Visual FoxPro Application for SQL Server..................................99 4 AVisual Basic .NET Framework..................................................................135 5 Adding Internet Access...............................................................................161 6 Data Access..................................................................................................227 7 XML.............................................................................................................281 8 Screen Design..............................................................................................325 9 Searching and Filtering...............................................................................385 10 Reporting.....................................................................................................435 Index...........................................................................................................483 Table of Contents Introduction 1 1 Differences Between Visual FoxPro and Visual Basic .NET 5 A Whole New World ......................................................................................5 The Integrated Development Environment (IDE) ........................................8 Typing Commands in the Command Window ...................................8 The Toolbox ..........................................................................................9 Tab Ordering .......................................................................................11 Projects and Solutions ........................................................................11 Compiler Output Formats ..................................................................13 The Code Window ..............................................................................14 The Document View Window ............................................................14 Bookmarks and Task Lists ...................................................................15 The Server Explorer ............................................................................15 What Can You DOin the IDE? ............................................................16 The Class Browser and Visual Basic .NET’s Class View ......................17 The Object Browser ............................................................................17 The Properties Window ......................................................................17 Getters and Setters Versus ASSIGNandACCESSMethods ...................18 Shortcut Keys ......................................................................................19 The Tools, Options Dialog ..................................................................20 The Task Pane Manager ......................................................................21 The Output Window and Your Application Menu ............................21 Customizing the IDE ..........................................................................22 The Visual Basic .NET Solution Explorer .....................................................22 Intermediate Language .......................................................................24 DLLs ....................................................................................................25 Other Characteristics of a Visual FoxPro Project ...............................25 Using Tables to Store FoxPro Project Components ...........................26 File Types Used in Visual Basic Projects .............................................27 Project Information ............................................................................28 Command Syntax ........................................................................................28 Flow of Control ............................................................................................30 Calling a Function, Procedure, or Method ........................................30 String handling ...................................................................................31 Ending Your Program .........................................................................31 Do...ENDDOand Other Loops in Visual FoxPro .................................31 Loops in Visual Basic .NET ...........................................................32 Conditional Execution in Visual FoxPro ...........................................33 Conditional Execution in Visual Basic .NET .....................................34 Variables, Enums, Arrays, Collections, and Fields ......................................35 Data Types ..........................................................................................35 Variable Scope Declaration in Visual FoxPro .....................................36 Variable Declarations in Visual Basic .NET ........................................38 Enumerations, Arrays, and Collections ..............................................40 Fields ...................................................................................................44 Functions and Subroutines (Procedures) .....................................................44 Function Declarations ........................................................................45 Summary of Visual Basic Declarations ...............................................50 Classes, Namespaces, Properties, and Forms ...............................................51 Instantiating objects in Visual Basic .NET .........................................56 Property Procedures in Visual Basic .NET ..........................................56 Forms ..................................................................................................58 Events ...........................................................................................................58 Events in Visual FoxPro ......................................................................59 Events in Visual Basic .NET ................................................................60 RaiseEvent ..........................................................................................62 How to Declare an Event in a Class ...................................................62 The Form .............................................................................................66 Compiler Directives .....................................................................................70 Data ..............................................................................................................71 Data Binding ................................................................................................74 Error Handling and Debugging ...................................................................75 TRY...CATCH ........................................................................................75 Debugging ...........................................................................................76 FoxPro Debugging Aids ......................................................................76 Visual Basic .NET Debugging Aids .....................................................77 Summary ......................................................................................................77 vi Visual FoxPro to Visual Basic .NET Side by Side 2 Building Simple Applications in Visual FoxPro 79 Building a Simple Application in Visual FoxPro..........................................79 Creating a Simple Form Controls Class Library ................................81 Field Mapping .....................................................................................81 Creating the Form Using Your Subclassed Controls ..........................82 Form Methods ....................................................................................82 Adding the Command Buttons ..........................................................83 Creating a Form Template ..................................................................87 Running the Form ..............................................................................89 Building a Simple Application in Visual Basic .NET ...................................89 Creating a Customers Table ...............................................................89 Designing the Form ............................................................................92 Creating a Dataset ..............................................................................92 Building the Form ..............................................................................93 Form Methods ....................................................................................93 Adding the Command Buttons ..........................................................94 Summary ......................................................................................................97 3 Building a Visual FoxPro Application for SQL Server 99 Why Three-Tier? ..........................................................................................99 Getting Our Test Data Ready ............................................................102 Creating the SQL Database and Loading Your Tables ...............................103 Writing the Sample Application ................................................................107 The Form Template ....................................................................................112 How to Use the FlatFileFormTemplate .........................................116 A Search Form Template ............................................................................117 How to Use the EasySearchTemplate .............................................122 The Data Tier .............................................................................................122 Running the Application ..................................................................133 What’s Next? ..............................................................................................134 4 AVisual Basic .NET Framework 135 Starting the New Windows Application Project .......................................135 The AppScreen Form Properties .......................................................137 Adding a Windows Controls Library .........................................................137 Building Your First Inheritable Form .........................................................141 Coding the Form Class .....................................................................144 Programming with Class ..........................................................................146 Automating Data Access ..................................................................147 Loading the List Box and Displaying a Record When Clicked ......150 Utility Routines ...............................................................................153 Contents vii ClickEvent Code for the Form’s Buttons .................................................154 How the Button Code Works ...........................................................155 How to Use This Template ...............................................................158 Summary ....................................................................................................159 5 Adding Internet Access 161 ASP and Database Development ...............................................................161 Internet Access in Visual FoxPro 7 ............................................................164 Installing Web Connection ..............................................................164 Building Your Application ................................................................165 The Main Program ............................................................................166 TheStandardFormClass Template .............................................................169 The Internet Server ...........................................................................170 How Web Connection works ...........................................................171 Writing Web Connection Functions .........................................................173 Passing Parameters ............................................................................173 An XML Primer .................................................................................177 The Customer Form ..........................................................................178 Introducing the Server ...............................................................................180 TheSEND2DBFandSEND2SQLFunctions ............................................192 Database Design Still Matters ...........................................................194 Modifying the Data Tier to Add Internet Data Access ..............................194 Building XML Web Services with Visual FoxPro 8 ....................................208 XML Web Services in Visual Basic .NET ....................................................213 A Main Form for the Sample Application ........................................213 Building a Web Service for the Sample Application ........................214 Changes to the CONFIG.WEBFile .......................................................215 Web Service Functions ......................................................................215 How It Works ....................................................................................216 Adding References to the Web Service to Your Client ..............................217 Code for the Windows Application to Use the Web Service ...........219 How It Works ....................................................................................220 The EditCustomer Form ...................................................................221 How It Works ....................................................................................223 Using Remoting Instead of Web Services .........................................224 Summary ....................................................................................................224 viii Visual FoxPro to Visual Basic .NET Side by Side 6 Data Access 227 Data Access in Visual FoxPro Before Visual FoxPro 8 ...............................228 The DBF Format ................................................................................228 Creating a Table ................................................................................229 Using Local Tables (DBFs) ................................................................229 Aliases ...............................................................................................230 Cursors ..............................................................................................231 Supporting Cast ................................................................................231 Buffering ...........................................................................................232 Indexes ..............................................................................................233 Database Containers ..................................................................................233 Local Views .......................................................................................235 Remote Views ...................................................................................235 SQL Pass-Through .............................................................................235 Connections and Connection Strings ..............................................236 SQLExec() .........................................................................................237 Other SQL Commands .....................................................................237 The Upsizing Wizard ........................................................................239 New Features in Visual FoxPro 8 ...............................................................239 TheCursorAdapterClass .................................................................239 TheXMLAdapterClass .......................................................................247 FoxPro Data Access Notes and Comments ......................................254 Data Access in Visual Basic .NET ...............................................................254 Disconnected Data Access ................................................................254 Connections .....................................................................................255 Data Adapters ...................................................................................256 Datasets .............................................................................................261 Typed Datasets ..................................................................................262 The Data Form Wizard .....................................................................263 The Generated Code .........................................................................268 Data Binding .....................................................................................271 XML Web Services ............................................................................274 How to Use the Web Service ............................................................277 Summary ....................................................................................................279 7 XML 281 The Structure of XML ................................................................................282 Encoding ...........................................................................................284 Namespaces .......................................................................................284 Contents ix Data Models: XDR and XSD .............................................................285 Examples of XML .............................................................................285 Representation of Complex Structures ......................................................287 Hierarchical XML .......................................................................................290 The Document Object Model ....................................................................293 Commands to Move Data Between a Table and XML ..............................295 CursorToXML() ..................................................................................295 XMLToCursor() ..................................................................................299 How FoxPro Implements XML ..................................................................302 Using XML to Read Other Types of Data .........................................302 Receiving XML Files Directly ...........................................................303 Importing XML into Visual Basic .NET .....................................................304 Direct Access to SQL Server with XML ............................................304 Using the XML DOM to Call a Web Service ....................................306 Using the XML DOM to Validate Documents .................................308 XMLUpdateGrams ...............................................................................309 DiffGramsin Visual FoxPro 8 ...........................................................312 The Visual FoxPro CursorAdapter ....................................................314 Creating a CursorAdapter to Read XML ..........................................316 Reading XML into a Visual Basic .NET Dataset ........................................320 The Visual FoxPro XMLAdapter .........................................................320 Using XML and Data Islands to Reduce Server Load ................................321 Data Islands ......................................................................................321 Related Technologies: XPATH and XSLT ...................................................323 Extensible Style Sheet Transformations (XSLT) .........................................324 Summary ....................................................................................................324 8 Screen Design 325 The Base Control Classes ...........................................................................325 Creating the Startup Screen for Your Application ....................................327 The Startup Screen for Your Visual FoxPro Application ..................328 The Startup Screen for Your Visual Basic .NET Application ............328 Creating Menus ..........................................................................................330 The FoxPro Menu .............................................................................330 The Visual Basic .NET MainMenuControl .........................................331 Traversing the Controls in a Form ...................................................332 Subclassing Controls ..................................................................................332 Subclassing the FoxPro Screen Controls ..........................................332 Using Your Subclassed FoxPro Controls ..........................................333 Subclassing the Screen Controls in Visual Basic .NET .....................335