
- •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

988 Chapter 22 PROGRAMMING THE ADO.NET OBJECTS
excessively large, you can reload it from the database and save yourself some serious debugging. You can also copy the code generated by the wizard and reuse it.
Updating Tables Manually
To update the underlying table(s) from within a DataSet, you must call the DataAdapter object’s Update method. The Update method sends all the changes to the database, and through the appropriate stored procedures, the tables are updated. The DataAdapter object exposes the ContinueUpdateOnError property, which determines how the DataAdapter will react when an error is encountered. If the ContinueUpdateOnError property is False, which (mysteriously) is the default value, the DataAdapter will terminate the update process. If the first 10 edited rows in the DataSet contain no errors, they will be committed to the database. If the 11th row contains an error, the DataAdapter won’t even attempt to update the remaining rows.
The ContinueUpdateOnError property should be set to True, so that the DataAdapter will update all the rows that don’t contain errors. My guess is that all developers will make a habit of setting this property to True. After calling the Update method with the ContinueUpdateOnError property set to True, you must examine each row’s Errors property to find out whether there was an error. Even better, you can insert the appropriate code in the DataAdapter object’s RowUpdated event, which is fired after each attempt to update a row.
In this event, you can examine the e.Status property for the current operation. If the operation failed, then the e.Status property will be UpdateStatus.ErrorsOccurred. You can then retrieve the error message describing why the operation failed and present it along with the row (or the row’s key field) to the user, or take some other course of action. There are no simple rules as to how to handle update errors; it all depends on the application. A safe approach is to accept the changes in rows of the DataSet that successfully updated the underlying table(s) and reset the ones that failed to update the database to their original values. At the same time, you can create a list of all the rows that failed to update along with the corresponding error message. Let’s see how to use this technique to build a robust data-entry screen.
The DataEntry project’s main form, shown in Figure 22.4, displays the names of all products in a ListBox control. To view the fields of a product on the form, click the product’s name in the list. The technique of using a ListBox control as a navigational tool has been described earlier in this chapter. There are actually better techniques for selecting a specific row, but the focus in this example is how to edit a table of the DataSet and commit the changes to the database.
Figure 22.4
The DataEntry project demonstrates robust table-editing techniques.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

PERFORMING UPDATE OPERATIONS 989
To edit a row, you just change the values of its fields on the form. To change the product’s category and supplier, simply select another item in the corresponding ComboBox control. To delete the selected product, click the Delete Product button. To add a new product to the table, click the New Product button. When this button is clicked, the add and delete buttons are replaced by OK and Cancel buttons. Moreover, the ListBox control is disabled, so that you can’t select another row until you finish editing the new row. To commit the new row to the DataSet, click OK. This action hides the OK and Cancel buttons and restored the other two buttons on the form.
Notice that the new row is added to the DataSet, which resides on the client—no information is sent to the database server. After editing, deleting, and adding rows to the DataSet, you can commit the changes to the Products table of the DataSet by clicking the Update Table button. In this button’s event handler, the Update method of the matching DataAdapter object is called. Instead of passing the entire DataSet to the server, we call the Update method three times, passing one set of rows at a time: the edited, the added, and finally the deleted rows. Listing 22.12 is the handler of the Click event of the Update Table button.
Listing 22.12: Calling the DataAdapter’s Update Method
Private Sub UpdateTable(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnUpdate.Click
If Not DsProducts1.GetChanges(DataRowState.Modified) Is Nothing Then _ SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Modified))
If Not DsProducts1.GetChanges(DataRowState.Added) Is Nothing Then _ SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Added))
If Not DsProducts1.GetChanges(DataRowState.Deleted) Is Nothing Then _ SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Deleted))
End Sub
The code calls the Update method with a different group of rows, only if the group isn’t empty. If there are rows to be updated, the Update method is called with a DataSet object that contains the edited (modified) rows. The GetChanges method retrieves the rows whose state is the same as the second argument.
The DataAdapter attempts to update one row at a time. After each attempt (whether it was successful or not), it fires the RowUpdated event, which is handled by the code shown in Listing 22.13.
Listing 22.13: Handling the DataAdapter’s RowUpdated Event
Private Sub SqlDataAdapter1_RowUpdated(ByVal sender As Object, _
ByVal e As System.Data.SqlClient.SqlRowUpdatedEventArgs) _ Handles SqlDataAdapter1.RowUpdated
Select Case e.Row.RowState Case DataRowState.Added
Console.Write(“Adding Product ID = “ & _ e.Row.Item(“ProductID”, ataRowVersion.Proposed))
Case DataRowState.Deleted
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

990 Chapter 22 PROGRAMMING THE ADO.NET OBJECTS
Console.Write(“Deleting Product ID = “ & _ e.Row.Item(“ProductID”, DataRowVersion.Original))
Case DataRowState.Modified Console.Write(“Updating Product ID = “ & _
e.Row.Item(“ProductID”, DataRowVersion.Original))
End Select
If e.Status = UpdateStatus.ErrorsOccurred Then Console.WriteLine(“Failed to update row “) Console.WriteLine(e.Errors.Message) e.Row.RejectChanges()
Else
Console.WriteLine(“ Updated successfully”) e.Row.AcceptChanges()
End If End Sub
The event reports the row being updated through the Row property of its e argument. The expression e.Row.RowState returns the state of the row. We use the row’s state to display the type of action (add/edit/delete) that caused the current row to be updated. We also display the key field’s value of the current row, so that the user knows which rows failed to update the database. Notice that new rows don’t have an original version, so we display the proposed version.
Then the code examines the e.Status property, which will be set to UpdateStatus.ErrorsOccurred if the operation failed, and display the message returned by the database. The output produced by the RowUpdated event handler looks like this:
Updating Product ID = 1 Updated successfully
Updating Product ID = 9 Updated successfully
Updating Product ID = 44 Updated successfully
Updating Product ID = 91 Updated successfully
Deleting Product ID = 78 Updated successfully
The product with ID of 91 was added to the table, but the RowUpdated event handler reports it as being updated.
For each row that successfully updates the underlying table, we call the AcceptChanges method. This method copies the original field values into the current values. For all intents and purposes, this row looks as if it was just read from the database and hasn’t been edited yet.
This techniques works only if the SqlDataAdapter1 object’s ContinueUpdateOnError property is set to True. If not, the update process will be interrupted the first time an error occurs. The project uses three DataAdapter objects, one for each of the tables Products, Categories, and Suppliers.
Of the three DataAdapter objects, only the one that corresponds to the Products table contains statements to update the database. The other two DataAdapters contain only a SELECT statement to retrieve their data from the database.
To add a new row to the Products table, you must click the New Product button. This button’s event handler prepares the various controls to accept the field values of the new row and displays the
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

PERFORMING UPDATE OPERATIONS 991
OK and Cancel buttons. Most important, it suspends the data binding. The TextBox controls on the form may assume invalid values for a while, so we must disable the data binding while the fields of the new row are being set. Data binding will resume later, when the user clicks the OK button and the new row is actually added to the DataSet. Here’s the code behind the New Product button:
Private Sub bttnNew_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnNew.Click Me.BindingContext(DsProducts1, “Products”).SuspendBinding() ListBox1.Enabled = False
bttnOK.Visible = True bttnCancel.Visible = True bttnDelete.Visible = False bttnNew.Visible = False
End Sub
Finally, when the OK button is clicked, the new row is committed to the database with the statements of Listing 22.14.
Listing 22.14: Adding a New Row
Private Sub bttnOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnOK.Click Dim newRow As DSProducts.ProductsRow
newRow = DsProducts1.Products.NewProductsRow newRow.Item(“ProductName”) = TextBox2.Text newRow.Item(“UnitPrice”) = TextBox3.Text
If cmbCategory.SelectedValue > 0 Then _ newRow.Item(“CategoryID”) = cmbCategory.SelectedValue
If cmbSupplier.SelectedValue > 0 Then _ newRow.Item(“SupplierID”) = cmbSupplier.SelectedValue
newRow.Item(“Discontinued”) = 0 DsProducts1.Products.AddProductsRow(newRow) Me.BindingContext(DsProducts1, “Products”).ResumeBinding() ListBox1.Enabled = True
bttnOK.Visible = False bttnCancel.Visible = False bttnDelete.Visible = True bttnNew.Visible = True
End Sub
To add a new row, we must first create a variable that represents this row. Since we’re working with a typed DataSet, we can declare a variable of the NewProductsRow type. Then we set the fields of the new row and finally add the newly created row to the table with the AddProductsRow method. After adding the row to the table, we enable the data binding again with the ResumeBinding method.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |