Hi,
As part of my learning of VB I am currently looking at Form Field Data validation.My question is :
Should I validate the form field input using the row changing or column changing event
I am adding a new datarow(record) to the dataTable and need to validate user input before committing the row to the underlying database. It looks to me that the dataColumnChagning is the one to use as it seems the quickest.
Although my code works I would like to know which would be the best, if it makes any difference.
Ron
Partial
Class Database1DataSet Partial Class dataDataTable Private Sub dataDataTable_dataRowChanging(ByVal sender As System.Object, ByVal e As dataRowChangeEvent) Handles Me.dataRowChanging ' \\ The following code will validate the Account column and if its value is less than 0 If CType(e.Row.Account, Integer) <= 0 Then 'e.Row.SetColumnError("account", "You must enter value more than zero")MsgBox(
"bad input") Elsee.Row.SetColumnError(
"account", "") End If End Sub
Private
Sub dataDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging If (e.Column.ColumnName = Me.AccountColumn.ColumnName) Then If CType(e.ProposedValue, Short) <= 0 Then 'e.Row.SetColumnError(e.Column, "Quantity must be greater than 0")MsgBox(
"Bad Input") '\\ IF BAD INPUT SET ADDRECORD TO FALSE '\\ THIS WILL PREVENT THE UPDATE FROM TAKING PLACE When executing the update method Me.addRecord = False Elsee.Row.SetColumnError(e.Column,
"") End If End If End SubEnd Class
If at first you don't succeed try try again!!!

Form Field data validation before committing to database
VIPERRWD
What NO REPLY's!!!!!
Anyway after more reading testing I have decided that this is the way I will validate my
form field input before adding the new row(record) to the dataTable and database:
I know that there is no wrong or right way to write code, but if there is a more efficent, eaiser way of doing this please can someone let me know.
I have decided to pass my input form field values to a class which will raise an event if the input value is bad. The event handler will then, depending on the class property that failed, prompt the user to correct the field input that is bad.
The following is the class that will be passed the values from the input form. If the set property for the account field value =1 then raise the event. The event handler is coded in the input form.
Public
Class customer Public Event myevent(ByVal myevent1 As String, ByVal state As Integer) Private m_FirstName As String Private m_LastName As String Private m_Account As Integer Private m_Company As String Private m_Contact As String Private m_Address As String Private m_PostCode As String Private m_Telephone As Integer Private m_Payment As String
Public Property FirstName() As String Get Return m_FirstName End Get Set(ByVal value As String)m_FirstName = value
End Set End Property Public Property LastName() As String Get Return m_LastName End Get Set(ByVal value As String)m_LastName = value
End Set End Property'\\ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Public Property Account() As Integer Get Return m_Account End Get Set(ByVal value As Integer) If value = 1 Then '\\ Raise event if value of Account=1 passing in value of Account and 1 to indicate which '\\ property raised the event. RaiseEvent myevent(CStr(value), 1) Elsem_Account = value
End If End Set End Property'\\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'\\ INPUT FORM
Public
Class Input Private firstname As String = "ron" Private WithEvents objcustomer As New customer Private addrecord As Boolean = True'\\ When the Input Form Save Button is pressed......
Private Sub btnSaveAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAcc.Click
'\\ Pass values of Form fields to customer class Properties set clause With objcustomer.FirstName =
"ron".LastName =
"nash".Account =
CInt(Me.txtAccount.Text).Company =
Me.txtCompany.Text.Contact =
Me.txtContact.Text.Address =
Me.txtAddress.Text.PostCode =
Me.txtPostCode.Text.Telephone =
CInt(Me.txtTelephone.Text).Payment =
Me.cboPaymeny.SelectedItem.ToString Dim addrecordAdapter As New Database1DataSetTableAdapters.dataTableAdapter Dim addrecordTable As New Database1DataSet.dataDataTableaddrecordAdapter.Fill(addrecordTable)
'\\ "Add record" will = false if validation fails
If addrecord = True Then Dim response As Boolean = CBool((MsgBox("Are you sure you", MsgBoxStyle.OkCancel, "Account Creation"))) If response = True Then '\\ fill new row with data from the customer class and update databseaddrecordTable.AdddataRow(.FirstName, .LastName, .Account, .Address, .Company, .Contact, .PostCode, .Telephone, .Payment)
addrecordAdapter.Update(addrecordTable)
ElseIf addrecord= false set objcustomer to nothing
objcustomer =
Nothing End If End If End WithEnd Sub
'\\ Following handles the raised event that is raised in customer class, objcustomer is an instance of the customer class. myevent1 will = the value of the form field value, state will = the customer class property that raised it.
Private Sub myeventhandler(ByVal myevent1 As String, ByVal state As Integer) Handles objcustomer.myevent Select Case state Case 1MsgBox(
"You need to enter a valid Account Number", MsgBoxStyle.Exclamation, "Account Error") Me.addrecord = False With Me.txtAccount.Text =
"".Focus()
End With Case 2MsgBox(
"Company Name =" + myevent1, MsgBoxStyle.Exclamation, "Company Error")'\\ Add code to handle this error here
End Select End SubEnd
ClassWPFMonkey
For those newbie VB developers like me, the following code will validate the textbox input on the keypress event.
The following will only allow input 0-9. If any other key is pressed it will not be allowed or displayed within the Textbox. By changing the values within the strValid variable you can allow/dis-allow input. By removing the NOT from the if not..... you can reverse it allowing all except numeric input.
Private Sub txtAccount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAccount.KeyPress Dim strValid As StringstrValid =
"01234567890" If Not CBool(InStr(strValid, Chr(AscW(e.KeyChar)))) ThenDebug.Write(e.KeyChar)
e.KeyChar = ChrW(0)
End If End SubRon
If at first you don't succeed try try again!!!!