Question validating problem

adshocker

Well-known member
Joined
Jun 30, 2007
Messages
180
Programming Experience
Beginner
hi all,

i created a validation procedure on a textbox_validating event. the procedure goes something like...

VB.NET:
Try
If Me.Textbox1.Text = String.Empty Then
  Throw New Exception("Textbox1 cannot be null")
End If
Catch ex as Exception
  MessageBox.Show(ex.Message)
End Try

but somehow it doesn't stop the system from continuing.. what happens is on my Save Button where i have...

VB.NET:
Me.Validate

Me.BindingSource1.EndEdit
Me.DataAdapter1.Update(Me.DataSet1.DataTable1)

i'm assuming that when i click the save button, the Textbox1_Validating event is also fired since it also brings up the MessageBox. but then after pressing OK on the MessageBox, it continues to try and Update the DataTable where I get an error from the database saying that the Column does not allow null values.

also when i click Save Button, i noticed that the Textbox1_Validating Event is only fired when the cursor is on that item. basing from this, it seems that the not all Validating Events are fired when an Adapter.Update is executed.

what's the best way to validate items? should it be on the Save Button and not on the Validating event of each items?

thanks
 
In the Validating event set e.Cancel to True if the validation fails, that way focus never leaves the control
 
Also, you definitely shouldn't be throwing an exception there. What's the point of throwing an exception and catching it in the same place? All that does waste resources. Just put the MessageBox inside the If block and get rid of all the exception-related stuff.

Of course, you need to follow JB's advice and put that in the If block too, but that's not enough. In your Save code you need to test the return value of Validate. It will be False if validation failed, in which case you do NOT go ahead and save.
 
hi all,

thanks for the tip.

the e.Cancel =True works perfectly for each Item Validation but still having a bit of a problem when saving records.

i have this for my item...

VB.NET:
    Private Sub UsernameTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles UsernameTextBox.Validating
        If Me.UsernameTextBox.Text = String.Empty Then
            MessageBox.Show("Username cannot be null")
            e.Cancel = True
        End If
    End Sub

    Private Sub Last_nameTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Last_nameTextBox.Validating
        If Me.Last_nameTextBox.Text = String.Empty Then
            MessageBox.Show("Lastname cannot be null")
            e.Cancel = True
        End If
    End Sub

so then for my Save Button i should have something like this?

VB.NET:
Private Sub SaveRecord()
  If Me.UsernameTextbox.Text = String.Empty Then
    MessageBox.Show("Username cannot be null")
  ElseIf Me.Last_nameTextBox.Text = String.Empty Then
    MessageBox.Show("Lastname cannot be null")
  Else
    Me.Validate()
    Me.BindingSource1.EndEdit()
    Me.DataAdapter1.Update(Me.DataSet1.DataTable1)
  End If
End Sub

or is there a better way of doing this?

thanks.
 
You've ignored this part of my last post:
In your Save code you need to test the return value of Validate. It will be False if validation failed, in which case you do NOT go ahead and save.
 
Is there any reason to check for missing data twice in both the validation event and also in the save event?
 
Is there any reason to check for missing data twice in both the validation event and also in the save event?
No, there isn't. The whole point of calling Validate is to raise the Validating events of the controls and catch any invalid data there. Of course, if you ignore the result and save regardless then it doesn't help much.
 
Actually, it just occurred to me that Validate will only validate the last control that had focus if it hasn't already been validated. To validate the whole form you need to call ValidateChildren.
 
hi,

Me.ValidateChildren worked well for validating the items.

so after reading your posts, i changed my code to this

VB.NET:
Private Sub SaveRecord()
  If Me.ValidateChildren () Then
    Me.BindingSource1.EndEdit()
    Me.DataAdapter1.Update(Me.DataSet1.DataTable1)
  End If
End Sub

so far so good.

thanks all.
 
hi,

i would just like to add a follow up question...

how to check for validation of cells in datagrid view?

somehow the Me.ValidateChildren() does not affect Cells in a datagridview.

thanks.
 
hi,

thanks..

the DataGridView.EndEdit is doing like what you said but i have a problem with my validating event. this is i think more like a coding problem.

VB.NET:
    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        If e.ColumnIndex = 0 Then
            If e.FormattedValue = String.Empty Then
                ShowMessage("Item cannot be null")
                e.Cancel = True
            End If
        End If
    End Sub

then i have the ff: on the save button...
VB.NET:
Private Sub SaveRecord()
  If Me.ValidateChildren () Then
    If Me.DataGridView1.EndEdit Then
      Me.BindingSource1.EndEdit()
      Me.DataAdapter1.Update(Me.DataSet1.DataTable1)
   End If
  End If
End Sub

this works if the focus is on the cell with ColumnIndex=0 only.

i think my logic is wrong on the CellValidating event... or should i be putting it on the RowValidating event instead?
 
hi,

i just found out that the validation events are fired when i close the form which causes the form not to close if validate is false.

how do i go pass this when closing the form?

thanks
 
nevermind.. i got the logic wrong on the FormClosing event. :D
 
Back
Top