- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary
970 Chapter 22 PROGRAMMING THE ADO.NET OBJECTS
The answer to all these questions is to update the database as frequently as possible through stored procedures. If a row fails to update, we must notify the user immediately, giving the user a chance to review the error and make the necessary corrections. But this approach violates the whole idea of using disconnected DataSets. Indeed, DataSets are a great tool for a disconnected world, but not all of our applications are disconnected. An application that runs in a small business environment isn’t going to benefit much from the disconnected nature of ADO.NET. It’s very likely that ADO.NET will be augmented in the near future to support connected DataSets as well. Until then, you must do most of the work manually, and you’ll see in this chapter how to update the underlying tables.
The DataForm Wizard
One of the tools that come with Visual Studio is the DataForm wizard, which creates data-entry forms for you. Let’s look at this tool in action, then we’ll discuss its limitations. You will also find interesting coding examples in the output generated by the wizard. The example of this section is the EditProducts project, whose main form is shown in Figure 22.2. This form allows you to edit the rows of the Products table of the Northwind database, enter new rows, and delete existing rows. The interface and the code behind the controls were generated by the DataForm wizard. What you see in Figure 22.2 is the form of the EditProducts project as it was generated by the wizard. The main form of the EditProducts project on the CD is quite different, because we’ll edit this form extensively in this section to make its interface more user-friendly.
Figure 22.2
Editing the Products table on a form generated by the DataForm wizard
Start a new project, name it EditProducts, and delete the Form1 component. Then right-click the project’s name and select Add Add New Item. In the dialog box that appears, select DataForm Wizard. A wizard starts that will take you through the steps of setting up a new DataForm. The first screen displays a welcome message; click Next to skip it. On the next screen, you’re prompted to specify the DataSet on which the DataForm will be based. Since the project doesn’t contain a DataSet, specify the name of a new DataSet, which the wizard will create for you. Enter the name DSProducts and click Next to view the next screen of the wizard.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE DATAFORM WIZARD 971
You’re prompted to select a connection to a database or specify a new one. Select your connection to the Northwind database and click Next to see the Choose Tables Or Views screen. Here you will see a list of all tables and views in the database, and you can select one or more items on which the DataForm will be based. If you select multiple tables, make sure they’re related. Select the Products table and then click the button with the right arrow to add the Products table to the list of selected items. Each product has a related row in the Categories table and another related row in the Suppliers table. Add these two tables to the list of selected items and then click the Next button to see the next screen.
On the next screen of the wizard, you’re asked to establish relationships between tables. Establish two relationships here, one between categories and products and another one between suppliers and products. In the Name box, enter the name of the first relationship, CategoriesProducts. The Parent table of the relationship is the Categories table, and the Child table is Products. The Keys are the matching columns in each table, and they are the CategoryID columns of the two tables. Then click the button with the right arrow to add the relationship to the list of Relations.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
972 Chapter 22 PROGRAMMING THE ADO.NET OBJECTS
Establish another relationship between the Suppliers and Products tables in a similar manner and add it to the list of Relations. You have established the relations between the DataSet’s tables, and you can click Next to view the next screen of the wizard.
On the next screen of the wizard, you’re prompted to choose the tables and columns that will be stored in the DataForm. We will not display related rows (as we did with the DataGrid control in Chapter 21). We want to design a form that displays all the columns of the Products table, and you’ll see shortly how the other two tables will be used (you can guess that we’ll use them to look up the name of the category and supplier of each product, rather than displaying all categories and all suppliers).
By default, the wizard selects all the fields of the Products table. If you wanted to create a master/detail form, you’d have to specify the Detail table as well. For now click Next to see the next screen of the wizard.
On the last screen, you must select the display style. You can display all rows on a DataGrid control or create a new form with separate controls for each row. Check the radio button Single Record In Individual Controls, and the check boxes at the bottom of the window will be enabled. These check boxes allow you to specify whether the DataForm will contain an Add button (to add new rows to the Products table), a Delete button (to delete the current row), a Cancel button (to cancel any changes in the DataSet and reload all rows from the underlying table) and the Navigation controls (to move from row to row). Leave all the check boxes checked, as they are by default.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE DATAFORM WIZARD 973
Now click Finish to generate the DataForm. The wizard will add a new form to your project, populate it with the necessary TextBox controls to display the field of the current row in the Products table, and also generate the necessary code. As you can see, all the TextBox controls have the same width and you must adjust them according to the length of the fields they display.
The navigational buttons at the bottom of the form allow you to move to the first/last and previous/next rows in the table. You will see the navigational controls (as shown in Figure 22.2) only if you build the project following the guidelines presented here. The EditProducts project on the CD is an improved version of the form generated by the wizard, and you will see shortly how you can enhance the default user interface.
You can notice immediately that the navigational method of the DataForm wizard is totally inadequate. We can’t expect users to keep clicking the Next button until they hit the desired row. To make the navigational model a little more user-friendly, we’ll add a list with the product names, so that the user can quickly locate the desired product. Figure 22.3 shows the new interface we’re going to build based on the form generated by the wizard.
Figure 22.3
A more functional form for editing the products
First, make the form wider. Then grab all the controls on the form with the mouse and move them to the right to make room for a ListBox control on the left side of the form. Place a ListBox control on the form, as shown in Figure 22.3, and set its properties as follows:
Property |
Value |
DataSource |
objDSProducts.Products |
DisplayMember |
ProductName |
ValueMember |
ProductID |
This ListBox will be populated with the names of all products in the Products list. The ValueMember property stores the IDs of the products, so that we can retrieve the selected product’s details by the ProductID field when the user selects a product on the list. Then enter the line of Listing 22.4 in the ListBox control’s SelectedIndexChanged event handler. This event is fired every time the user clicks another item on the control, and in its handler we’ll add code to move to the selected product’s row in the Products table in our DataSet. The various TextBox controls on the Form are bound to the corresponding columns of the Products table. As a result, when we move to another row of the Products, the text boxes are updated.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
974 Chapter 22 PROGRAMMING THE ADO.NET OBJECTS
Listing 22.4: Moving to the Selected Product
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Me.BindingContext(objDSProducts, “Products”).Position = ListBox1.SelectedIndex
End Sub
We no longer need the so-called navigation buttons at the bottom of the form, so delete them. Delete the Label control that displays the current row’s number as well—we can easily visualize our approximate position in the table by looking at the ListBox control.
If you run the project now, you’ll be able to quickly locate a product in the ListBox control and click it to view its fields. Before you run the project, however, you must specify that the DataForm1 form is its Startup object. The form behaves as expected most of time, but there are a few quirks to this design. The product’s supplier and category are displayed as numeric values. Sure, this is how we want to store the information to our database, but when we design forms for the end user, we want to be able to show them a supplier name and a category name. We’ve been through this in the previous chapter, so you already know how to replace these two TextBox controls with ComboBox controls that display the equivalent strings. Delete the TextBox control that corresponds to the SupplierID field. Then place a ComboBox control in its place and set the following properties of the control:
Property |
Value |
DataSource |
objDSProducts.Suppliers |
DataMember |
CompanyName |
ValueMember |
SupplierID |
SelectedValue |
objDSProducts - Products.SupplierID |
Delete the TextBox control that displays the CategoryID and replace it with a ComboBox control with the following settings:
Property |
Value |
DataSource |
objDSProducts.Categories |
DataMember |
CategoryName |
ValueMember |
CategoryID |
SelectedValue |
objDSProducts - Products.CategoryID |
Run the project now, load the DataSet, and edit a few rows. You have a functional interface, and you can edit the rows of a table in the DataSet. My advice is to disallow the editing of the current row in browsing mode by turning on the ReadOnly property of the TextBox controls. Place an Edit button on the form, which will make the controls editable by resetting their ReadOnly property. While the user can edit the current row, disable all navigational controls on the form and force users to end the edit operation by clicking the OK or Cancel button.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE DATAFORM WIZARD 975
The new interface is much more functional than the original one, produced by the wizard, but it’s not very robust. Let’s say you wish to take a look at the suppliers, or categories, by expanding the appropriate ComboBox control. It sure isn’t your intention to change the current row. Click a category name by mistake and you’ve edited the current row. When you move to the next row, the changes will be committed to the DataSet, and when you click the Update button the changes will be sent to the database. Sure, my suggested approach takes a few more keystrokes, or mouse clicks, but real-world applications must be robust.
Let’s continue our exploration of the application the wizard has generated for us. Try adding a few products. Don’t bother specifying the new product’s ID; it will be assigned one by the database. The Add button will add a new row to the Products table in the local DataSet. The new rows will be submitted to the database along with the edits and deletions (if any) when you click the Update button.
When adding a new product, make sure you click the CheckBox control (field Discontinued) once to check it, and once again to clear it. The default state of this control isn’t cleared, even though you don’t see a check mark. If you don’t touch the CheckBox, the application will crash with an error message to the effect that the Discontinued field can’t be Null. The same will happen if you don’t supply a value to the ProductName field, because this field can’t be Null. As you can see, some of the table’s basic constraints are embedded into the DataSet’s DataTable object, and they’re enforced. This is a good thing, but should they crash the application?
The program crashes when it attempts to move to another row in the table and the current row has errors. We can trap this error with a structured error handler and prevent the program from crashing. Open the SelectedIndexChanged event handler of the ListBox control and insert the code shown in Listing 22.5.
Listing 22.5: Handling Data-Entry Errors
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Try
Me.BindingContext(objDSProducts, “Products”).Position = _ ListBox1.SelectedIndex
Catch dataException As Exception MsgBox(dataException.Message)
Me.BindingContext(objDSProducts, “Products”).CancelCurrentEdit() Me.BindingContext(objDSProducts, “Products”).Position = _
ListBox1.SelectedIndex End Try
End Sub
Note OK, the problem is the statement we inserted to improve the navigational capabilities of the DataForm, so it’s not really the wizard’s fault. But our custom code and the simplistic navigational tools inserted on the form by the wizard do the same thing—they change the Position property of the BindingContext object—so the original code would have failed in the same manner.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |