- •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
SUMMARY 1081
The ItemCommand event is raised when any button or hyperlink on the control is clicked; that’s why the code examines whether the event was fired by a pager item. In this case, the event handler is terminated. If the event was fired by a button, the code executes the DAOrders object’s SelectCommand passing the selected customer’s ID as argument and then fills the DSOrders1 DataSet. The last step is to refresh the DataGrid control with the orders by calling its DataBind method.
With similar statements, the third DataGrid control on the form is populated with the selected order’s detail lines:
Private Sub DataGrid2_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _ Handles DataGrid2.ItemCommand
Dim orderID As Integer = DataGrid2.DataKeys(e.Item.ItemIndex) DADetails.SelectCommand.Parameters(“@OrderID”).Value = orderID DADetails.Fill(DSDetails1, “Order Details”)
DataGrid3.DataBind() End Sub
For the sake of completeness, I’m including the listing of the control’s PageIndexChanged event handler, which enables the user to navigate to another page of the DSCustomers DataSet by clicking one of the page links at the bottom of the control:
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _ Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex DACustomers.Fill(DSCustomerNames1, “Customers”) DataGrid1.DataBind()
End Sub
The MasterDetail Web application is quite functional and is typical of the type of application you’ll be called to develop, along with order-processing applications. The code is minimal, and all the functionality required to build the application resides in the control itself. It will take you longer to set the properties of the DataGrid control than to actually write the code.
Summary
This chapter was about ADO.NET on the Web. We focused on data-binding techniques and the visual tools, because this is how you’re supposed to make the most of a rapid application development (RAD) environment like VB. Just about everything we did with point-and-click operations in this chapter can be done with code. As you familiarize yourself with both ASP.NET and ADO.NET, you’ll be able to switch between the visual tools and the code at will.
If you were asked to compare the ease of use of ADO.NET with Windows applications and Web applications, wouldn’t you agree that ADO.NET is better suited for Web applications? ADO.NET was designed for disconnected applications, and the only true disconnected applications are the ones that run on the Web. In Chapter 25, the last chapter of the book, you’ll learn yet another way to use ADO.NET on the Web, through Web services. Web services are the hottest topic in the industry (along with XML, of course).
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
Chapter 25
XML Web Services
The last topic discussed in this book is one of the hottest new features of Visual Studio
.NET, XML Web services, or simply Web services. A Web service is a Class that resides on a Web server and services requests, just like an ASP application. The difference is that the Web service doesn’t furnish HTML pages to the client. Instead, it behaves like a function: clients call the function by name, pass arguments (if needed), and get back a result. The result can be number, a string, an object like a DataSet, or an image. In the examples of this chapter, you’ll see how you can write Web services that return DataSets and how these DataSets can be used to populate DataGrids or other data-bound controls on the client. You can also save the DataSet’s rows to a local file in XML format and use it in other applications. This is actually a very efficient method of making data like price lists available to a large number of recipients.
This is literally a how-to chapter that shows you how to build Web services. No special programming background is required to build Web services; if you can write a function in VB, you can write a Web service. Of course, if your VB function must be executed in the environment of Internet Information Services (IIS), it must encode its result in a form suitable for transmission over the HTTP protocol so that it can respond to requests made over the Web. Theoretically, you can write an ASP application that does the same, but it’s not an easy task. Visual Studio makes building and using Web services as easy as writing and using VB functions in a Windows application. Visual Studio does a lot for you behind the scenes, so that you can think of Web services as regular classes that expose methods—except these methods are available on the Web.
How to Serve the Web
A Web service is a Class that exposes methods, which are in effect functions. To contact the services of a Web service, you must create an object variable that references the specific Web service, just as you can access the members of any class through a variable. Programmers and managers at Microsoft think that Web services will take the Web by storm and that they’ll change the way we program on the Web. It just might happen. You may remember the Web Classes (a tool for abstracting ASP applications that came with VB6). No real developer ever used this tool to build ASP pages; they would much rather build ASP pages with VBScript.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
1084 Chapter 25 XML WEB SERVICES
Web services are not clumsy, like Web Classes were. It’s a nicely implemented feature, and we should see many sites offering their services on the Web in the form of Web services. If you’re wondering what type of services you might offer on the Web, I’ll give a few examples momentarily. But first, I would like to make clear that Web services are nothing more than glorified classes or functions. If we pretend for a moment that we know nothing (or don’t care) about the complexity of the Web or the problems with firewalls and security, we should be asking ourselves: If we can write a VB function and use in multiple Windows applications, why not be able to do the same with the Web? If this question sounds reasonable to you, then Web services are just classes that make their functionality available on the Web.
To better understand the need for Web services, I’ll discuss a couple of examples. Let’s say you’re working for a publisher and you get requests from online stores for information about the books your company publishes. Webmasters need book descriptions, cover photos, anything that will help them better present your books on their site. You can create new documents and send them out on a daily basis, or come up a centralized mechanism for distributing the same information to multiple destinations. As you can guess, different sites may request this information in drastically different formats, from text or Excel files to XML documents.
The centralized mechanism is a business-to-business site that people can connect to and request information from. If they don’t have the cover photo for a specific book, they should be able to connect to this site and make a request like “send me the cover image of the book with ISBN=xxx.” This request should be implemented with a function like GetCoverByISBN(isbnNumber). The GetCoverByISBN() function will return an image, which the other site can either store on its server and reuse or embed directly in its output for a specific client. You can do something similar by opening the HTML page of the book and downloading the file with the cover photo. This means that you must write a bit of code to parse the page, locate the name of the file, and then download it from the server. If the layout of the page changes, you must also change the code that parses the page.
If you could provide a function that could be called over the Web and return a single GIF file, then every online store could use this function. Programmers could even insert the name of the function in the place where the image would normally appear on the page. This way, every time a visitor hits the page, the image will be requested from your server and embedded in their page.
Another example is a function that returns the current price of a product. Any online store that sells your products will offer up-to-date prices and specials without any special arrangements. Or you can post a list of special offers and know that other sites can grab this information and use it immediately.
Other more advanced examples include integrated financial services. A highly secure site could collect information from banks, brokers, and credit organizations and present a unified picture of your finances. We expect to see this type of application in the very near future. Whether the information will flow from Web service to Web service remains to be seen, but this is one of Microsoft’s contributions to an interconnected world of dissimilar computers.
But will other operating systems and Web servers be able to interact with Web services? The secret ingredients that make it all work are XML and SOAP (Simple Object Access Protocol). They’re both open standards and are not very difficult to implement. You can actually write an ASP page that returns one or more values in XML or SOAP format and send it to the client instead of an HTML page. These two formats allow you to send any type of information over the HTTP protocol and through firewalls, so if the other end is capable of handling the XML tag, it will be able to use the Web service. As of now, all major players are promising to support XML, and this makes Web services a very promising technology, because they’re based on standards.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
