您的位置:首页 > 其它

DataWindow.NET How To: Data Entry Form(检测表单字段是否不能为空)

2008-01-24 10:48 537 查看
DataWindow.NET How To: Data Entry Form
Simplifying database application development
By: Bruce Armstrong
Aug. 5, 2005 10:45 AM
Digg This!













Page 3 of 4 « previous page next page »

There's a similar method coded for the dw_product DataWindowControl. All the rows of data are available in that DataWindowControl, but only one row is shown. As a result, if the user tabs out on the last column of dw_product, or reverse tabs (shift-tab) on the first column, he or she will actually scroll that DataWindowControl into the next or prior row of data. The dw_product_RowFocusChanged method, as shown below, keeps the two views of the product data in synch by moving the row selection indicator on the dw_productlist DataWindowControl to match the newly selected product. Note that it also uses the LastRowOnPage and FirstRowOnPage properties to determine if the dw_productlist data has to be scrolled forwards or backward to ensure that the newly selected product is visible in that DataWindowControl.

dw_product_RowFocusChanged
If e.RowNumber > 0 And Not IsRetrieving Then
dw_productlist.SetRow(e.RowNumber)
If e.RowNumber > dw_productlist.LastRowOnPage Then
dw_productlist.ScrollToRow(e.RowNumber)
ElseIf e.RowNumber < dw_productlist.FirstRowOnPage Then
dw_productlist.ScrollToRow(e.RowNumber)
End If
End If

Let's assume that the user has made some changes to the product data and now wants to issue an update. With the VB.NET form, the btnUpdate_Click method first determines if the user is updating a new or existing row by checking the Mode variable, and then calls the AddProduct or UpdateProduct form methods, respectively. Once again, because the DataWindow keeps track of whether a row is being inserted or updated for us, the DW.NET version of the form only has to call its version of the UpdateProduct form method.

The VB.NET AddProduct and UpdateProduct methods contain roughly 50 lines of code each to parse together the insert or update statement needed to update the product. And, as with the .NET Pet Shop sample, no provision has been made for collision detection on the update. That is, every column is updated, whether modified or not, and the update is based solely on the primary key for the product table. If two more users try to update the same product, the last user to update wins, and no warning is given to any of the users that there might be an issue.

The DW.NET UpdateProduct method, while significantly smaller, addresses these issues. The DataWindow Object offers several options for determining how update statements are automatically generated (see Figure 6). One of them (the one used here) automatically generates a WHERE clause that includes all updateable columns and their original values. In there is an update collision, the second and subsequent users attempting to update the same data will be informed of the collision and their changes won't be applied.

DW.NET UpdateProduct
dw_product.AcceptText()
' Validate form values.
If Not IsValidForm() Then
Exit Sub
End If
Try
Dim conn As New System.Data.OleDb.OleDbConnection
conn.ConnectionString() = ConnectionString
conn.Open()
Dim SQLCA As New Sybase.DataWindow.AdoTransaction
SQLCA.Connection = conn
SQLCA.BindConnection()
dw_productlist.SetTransaction(SQLCA)
SQLCA.Transaction = SQLCA.Connection.BeginTransaction
dw_productlist.UpdateData()
SQLCA.Transaction.Commit()
SQLCA.Connection.Close()
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try

Both the VB.NET and the DW.NET UpdateProduct methods (and the VB.NET AddProduct method) call an IsValidForm method to determine if the user has provided all the data necessary before attempting the update. The VB.NET version of this method checks each of the individual controls to make sure they have been populated. The DW.NET version, as shown below, is not only smaller, it's also much more flexible:

DW.NET IsValidForm
Dim row As Integer = dw_product.CurrentRow
Dim column As Int16 = 1
dw_product.FindNextRequiredColumn(row, column, DataBuffer.Primary, True)
If row > 0 Then
Dim columnname As String = dw_product.GetColumnObjectByNumber(column).Name()
MsgBox("Please enter a value for " + columnname, MsgBoxStyle.Exclamation, Me.Text)
Return False
Else
Return True
End If

There's an attribute on a column edit style in the DataWindow Object to indicate whether the column is required. The DataWindowControl FindNextRequiredColumn method is then used to determine whether any of these required columns haven't been populated. Note that the method doesn't refer to any specific columns by name. The approach is quite generic, and can be applied to any DataWindow. Any customization of the behavior is handled by the attributes on the DataWindow Object.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: