PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder

PowerBuilder: Article

DataWindow.NET How To: Data Entry Form

Simplifying database application development

The one minor issue with this approach is that the DataWindowControl, by default, won't let the user tab out of columns that are marked as 'required' without entering data. It's often better to let the user move out of required columns and simply do the validation just before a database update. To do this, we add a dw_product_ItemError method, as shown below, that lets the user move out of the required columns without entering data. Note that this function is also quite generic and could be used on any DataWindow object.

DW.NET dw_product_ItemError
'by default the DataWindow will generate an error message
'when ever the user moves off a required column without populating it
'we want to delay that until the user is ready to submit the record
'to the database, but we also want to do the required field check
'dynamically using FindNextRequiredColumn, rather than checking

'is the new value an empty string
If e.Data() <> "" Then
e.Action = ItemErrorAction.Reject
Exit Sub
End If

'is the current value null
If dw_product.IsItemNull(e.RowNumber, e.ColumnName) Then
e.Action = ItemErrorAction.RejectAndAllowFocusChange
Exit Sub
End If

'otherwise set it to null
dw_product.SetItemNull(e.RowNumber, e.ColumnName)
e.Action = ItemErrorAction.RejectWithNoMessage

When users want to add a new product, they hit the Add command button and the btnAdd_Click method is called. In the VB.NET version, a ClearForm method is called that resets each of the individual controls to a default value. That instance variable that tracks the insert/update status is also set and the Add and Delete buttons are disabled. In the DW.NET version, we don't need a ClearForm method. Instead we simply do an InsertRow on the DataWindow control, and the columns in the newly created row automatically take the initial values provided in the DataWindow Object. Since the DataWindowControl is tracking the insert/update status for us, we also don't need that instance variable. And since the DataWindow control can handle updates on multiple rows simultaneously, we don't constrain the user by disabling the Add or Delete buttons either. The last thing we do in the DW.NET version of the btnAdd_Click method (as shown below) is scroll the dw_productlist DataWindow to make sure the newly inserted row is visible there as well as in the data-entry area.

Finally, we'll look at what happens when the user wants to delete a product. As you might have come to expect by now, the VB.NET DeleteProduct method contains 40 lines of code in which the SQL delete statement is constructed and executed. The DW.NET version of that method, as shown below, contains seven lines of code. We simply issue a DeleteRow on the DataWindowControl and then use the UpdateProduct method to handle the database update the same as we did for the inserts and updates.

DW.NET DeleteProduct
Dim row As Integer = dw_productlist.CurrentRow()
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
End Try

As we've seen, DataWindow.NET technology can be used to simplify the generation of WinForms application development when database interaction is involved. Once the DataWindow Object is generated from the SQL select statement, it automatically handles the generation of SQL insert, update, and delete statements. And the update statements generated by the DataWindow technology automatically deal with issues such as optimistic concurrency and update collisions. The DropDownDataWindow edit style relieves the developer of having to develop code to populated dropdownlistboxes. The ShareData functionality lets the same result set be used to populate different DataWindowControls that provide different views of the same data; even different rows in that result set. Finally, DataWindow Object edit style properties and DataWindowControl methods let us easily implement required data validation generically.

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.