Pro Dynamic .NET 4.0 Applications Data-Driven Programming for the .NET Framework ■ ■ ■ Carl Ganz, Jr. i Pro Dynamic .NET 4.0 Applications: Data-Driven Programming for the .NET Framework Copyright © 2010 by Carl Ganz, Jr. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2519-5 ISBN-13 (electronic): 978-1-4302-2520-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Matthew Moodie Technical Reviewer: Ryan Follmer Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom WelshProject Manager: Anita Castro Copy Editor: Tiffany Taylor Compositor: Bronkella Publishing LLC Indexer: John Collin Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii With all paternal love, to Carl John III, Rose Veronica, and our unborn baby, either Paul Christian or Emily Anne, whichever one you turn out to be. iii ■ CONTENTS Contents at a Glance Contents at a Glance.......................................................................... iv Contents ............................................................................................ v About the Author ............................................................................... ix About the Technical Reviewer.............................................................. x Acknowledgments.............................................................................. xi Introduction ..................................................................................... xii ■Chapter 1: Introducing Data-Driven Programming................................ 1 ■Chapter 2: Reflection...................................................................... 29 ■Chapter 3: Runtime Code Compilation .............................................. 59 ■Chapter 4: Dynamic WinForms ......................................................... 77 ■Chapter 5: Dynamic ASP.NET ..........................................................123 ■Chapter 6: Dynamic WPF ................................................................155 ■Chapter 7: Reporting .....................................................................183 ■Chapter 8: Database Design ...........................................................217 ■Index............................................................................................237 iv Contents Contents at a Glance.......................................................................... iv Contents ............................................................................................ v About the Author ............................................................................... ix About the Technical Reviewer.............................................................. x Acknowledgments.............................................................................. xi Introduction ..................................................................................... xii ■Chapter 1: Introducing Data-Driven Programming ...............................1 Database Metadata.........................................................................................................2 SQL Server.............................................................................................................................................2 Oracle.....................................................................................................................................................4 Practical Applications.....................................................................................................6 Code Generation..............................................................................................................9 Custom Code Generators.....................................................................................................................10 Using the CodeDOM.............................................................................................................................13 Summary.......................................................................................................................28 ■Chapter 2: Reflection...................................................................... 29 Instantiating Classes.....................................................................................................29 Loading Shared Assemblies.................................................................................................................31 Examining Classes...............................................................................................................................32 Drilling Down into Assembly Objects............................................................................41 Building an Object Hierarchy................................................................................................................42 Importing Control Definitions...............................................................................................................45 v ■ CONTENTS Decompiling Source Code.............................................................................................52 Summary.......................................................................................................................57 ■Chapter 3: Runtime Code Compilation .............................................. 59 System.CodeDom.Compiler Namespace.......................................................................59 Compiling the Code..............................................................................................................................61 Error Handling......................................................................................................................................63 Executing the Code..............................................................................................................................66 Referencing Controls on Forms.....................................................................................68 Adding References........................................................................................................70 Testing..........................................................................................................................75 Summary.......................................................................................................................75 ■Chapter 4: Dynamic WinForms......................................................... 77 Instantiating Forms.......................................................................................................77 Wiring Events................................................................................................................81 Loading Control Definitions...........................................................................................83 Loading from XML................................................................................................................................84 Loading from a Table...........................................................................................................................86 Connecting Event Code........................................................................................................................90 Practical Solutions........................................................................................................92 Building a Filter Screen........................................................................................................................92 Saving Grid Settings.............................................................................................................................99 Data-Driven Menus............................................................................................................................103 Creating Criteria Screens............................................................................................110 Dynamic Criteria Controls..................................................................................................................110 Extracting the User Selections...........................................................................................................117 Summary.....................................................................................................................122 vi ■ CONTENTS ■Chapter 5: Dynamic ASP.NET......................................................... 123 Instantiating Web Controls..........................................................................................123 Understanding the Page Life Cycle....................................................................................................127 Using HTML Tables.............................................................................................................................133 ParseControl.......................................................................................................................................137 Instantiating User Controls.................................................................................................................139 Repeater Controls..............................................................................................................................143 Practical Solutions......................................................................................................147 Dynamic Criteria Controls..................................................................................................................147 Extracting the User Selections...........................................................................................................152 Summary.....................................................................................................................153 ■Chapter 6: Dynamic WPF............................................................... 155 XAML...........................................................................................................................155 Layout.........................................................................................................................157 Canvas...............................................................................................................................................158 Grid.....................................................................................................................................................160 StackPanel.........................................................................................................................................162 WrapPanel..........................................................................................................................................164 DockPanel..........................................................................................................................................166 Runtime Instantiation..................................................................................................168 Accessing Child Controls....................................................................................................................171 Nested Controls..................................................................................................................................172 XamlWriter/XamlReader..............................................................................................175 Persisting Objects..............................................................................................................................176 IsAncestorOf/IsDescendantOf............................................................................................................177 Wiring Events..............................................................................................................179 Data-Driven .Menus....................................................................................................180 Summary.....................................................................................................................182 vii ■ CONTENTS ■Chapter 7: Reporting .................................................................... 183 SQL Server Extended Properties.................................................................................183 Microsoft Excel...........................................................................................................189 Syncfusion’s Essential XlsIO..............................................................................................................191 SoftArtisans’ OfficeWriter for Excel....................................................................................................194 PDF..............................................................................................................................197 iTextSharp..........................................................................................................................................197 Syncfusion’s Essential PDF................................................................................................................200 SAP/Business Objects: Crystal Reports.......................................................................202 Embedded vs. Nonembedded Reports...............................................................................................202 Dynamic Crystal Reports....................................................................................................................202 SQL Server Reporting Services...................................................................................210 Using RDL...........................................................................................................................................210 Dynamic Rdl.......................................................................................................................................212 Summary.....................................................................................................................216 ■Chapter 8: Database Design .......................................................... 217 Data Storage...............................................................................................................217 Committing Data to the Database...............................................................................221 Using Inverted Tables.................................................................................................225 Structuring Inverted Data...................................................................................................................225 Extracting Inverted Data.....................................................................................................................227 Converting Inverted Data to a Normalized Structure.........................................................................229 Mixing Normalized and Inverted Tables.............................................................................................232 Summary.....................................................................................................................235 ■Index........................................................................................... 237 viii About the Author ■ Carl Ganz, Jr. is a Senior Software Developer at Innovatix, LLC., in New York. He has an M.B.A in Finance from Seton Hall University and is the author of four other books on software development as well as dozens of articles on Visual Basic, C#, and Microsoft .NET technology. He is the president and founder of the New Jersey Visual Basic User Group and has been a featured speaker at software development conferences in both the U.S. and Germany. Carl and his wife Wendy, live in Raritan, New Jersey, with their son Carl III, their daughter Rose, and their cats Jack and Jake. Contact Carl at [email protected]. ix