From the Editor-in-Chief of PowerBuilder Developer's Journal

Bruce Armstrong

Subscribe to Bruce Armstrong: eMailAlertsEmail Alerts
Get Bruce Armstrong via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


DataWindow.NET How To: Data Entry Form

Simplifying database application development

Last month we looked at Microsoft's .NET Pet Shop sample application and saw how DataWindow.NET technology could be used in the data access layer of an ASP.NET-based application to reduce the its complexity and increase developer productivity.

This month we'll look at another Microsoft sample application to see what benefits DataWindow.NET technology can provide when used for data presentation and data access in a Windows Forms (WinForms) application.

To fully appreciate how much DataWindow.NET simplifies the development of database applications in Windows Forms, you'll need the Microsoft sample application as a reference point. In this case, you'll want the Visual Basic .NET Code Sample: Data Access - Data Entry Form available at www.microsoft.com/downloads/details.aspx? FamilyID=34e601ca-e4b8-43ac-bfa9-426483a392df&displaylang=en.

The sample connects to the Northwinds sample database either in MS SQL Sever or MSDE so you will need to have that installed too.

Once you have the Microsoft sample, add the sample code from this article to that same folder, and then add the frmDWMain.vb file to the project by right-clicking on the project, selecting "Add," and then "Add Existing Item" (as shown in Figure 1).

In the subsequent dialog, select the frmDWMain.vb file. It should now appear in the list of project files (as in Figure 2). You can now switch between the form from the original sample and the new form built with DataWindow.NET technology simply by switching the Startup Object for the project from the project property page (see Figure 3).

Try running the application first with the original form (see Figure 4) as the startup object and again with the DataWindow.NET form (see Figure 5) as the startup object. Compare how each form works to verify that they both have the same functionality. There are some minor differences, which we'll cover as we compare the code. But for the most part the only thing you should notice about the way they operate is the different titles on the forms and the fact that the DataWindow.NET version implements the newer XP visual style (if you're running on XP).

A Closer Look at the Code
There are some minor differences in the header of the two forms. We're going to use ADO.NET for the DataWindow.NET version, so we won't need to import System.Data.SQLClient as the VB.NET version does. As a result, our connect string is going to be slightly different, since we'll include a data provider, as shown below. The VB.NET version also has a couple of "instance" variables.

The HasConnected variable tracks whether the user has connected to the database, and is used in a function that manually populates the dropdown list boxes on the form. We'll be using the DataWindow's built-in DropDownDataWindow edit style, which automatically populates those to be used, so we don't need that variable or its related function.

Secondly, the VB.NET form has a Mode variable that tracks whether the user is editing a new or existing database record. Since the DataWindow also manages that for us automatically, we don't need to bother tracking the type of update.

VB.NET Connect String DW.NET
Connect String
As String = _
"Server=(local);" & _
"DataBase=Northwind;" & _
"Integrated Security=SSPI;Connect Timeout=5"
Protected Const
"Data Source=(local);" & _
"Provider=SQLOLEDB;" & _
"Initial Catalog=Northwind;"
& _
"Integrated Security=SSPI
;Connect Timeout=5;"

We're not going to get into the Windows Form Designer generated code except to note that there's a lot more of it with the VB.NET implementation. This is primarily because the VB.NET implementation used a separate windows control and text label for each of the 10 columns in the result set.

We're going to use two DataWindowControls, one for the product listbox and the other for all of the other data-entry controls. A DataWindowControl implementation appears as a single control in the Windows Form Designer creating one-tenth of the code in that section.

The first method that gets run on either version of the form is the frmDataEntry_Load method, the one loading the form. In the VB.NET version, there are calls to a PopulateCategoryCombo and a PopulateSupplierCombo method. Each of those has roughly 75 lines of code used to populate the two dropdownlistboxes in the VB.NET form. As I indicated above, we'll be using the DropDownDataWindow edit style for those dropdowns, so the DataWindow automatically handles their population. The DW.NET version doesn't have those methods.

In the DW.NET version of frmDataEntryLoad we have the assignment of the LibraryList and DataWindowObject properties of the two DataWindowControls on the form (though this could have been handled through the property editor as well). We're also doing a ShareData so that we can retrieve the product data into one of the DataWindowControls but display that same data in different layouts in both DataWindowControls. Both versions of that method then make a call to the PopulateProductList method. Note that the btnRefresh_Click method on both forms contains very similar code, at least with respect to the call to the three methods in the VB.NET version and the call to the PopulateProdcutList method only in the DW.NET version.

More Stories By Bruce Armstrong

Bruce Armstrong is a development lead with Integrated Data Services (www.get-integrated.com). A charter member of TeamSybase, he has been using PowerBuilder since version 1.0.B. He was a contributing author to SYS-CON's PowerBuilder 4.0 Secrets of the Masters and the editor of SAMs' PowerBuilder 9: Advanced Client/Server Development.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
alvin 07/23/08 08:36:24 PM EDT

how can i change the highlight color of selected row? the default is dark blue which is too heavy. thanks

Allan A. Cartagenas 08/18/05 04:38:53 AM EDT

Where can I download the file frmDWMain.vb? The link to download the file is not included on the article.