Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
sir! please help. this is my output:http://tinyurl.com/mvnfvlh[^]

an error provider shows up when user missed to enter a value to some fields so the system will not allow the user to save record to the database. user must fill all fields. assuming that the messagebox is my save code, after entering data to that field/s that needs to enter a value.Now, the problem is this messagebox always repeat according to how many fields that the user missed to enter a data. i dont know where to put this save and messagebox code. or some gives me a better solution for this

my code:

VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Try

            For Each item As Control In GroupBox1.Controls

                If item.Text = Nothing Then

                    ErrorProvider1.SetError(item, "enter a value")

                Else

                    ErrorProvider1.SetError(item, "")

                    ''code to save
                    ''

                    'Me.Validate()
                    'Me.UserprofileBindingSource.EndEdit()
                    'Me.TableAdapterManager.UpdateAll(Me.SampleDataSet)
                    MessageBox.Show("Saved", "System")
                End If

            Next

        Catch ex As Exception

        End Try

    End Sub
Posted

1 solution

Firstly, this is not the best approach to screen validation. You should take advantage of controls "Validating" and "Validated" methods. But based on what you have done, I would do something as follows:

VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Try

            Dim PassedValidation as Boolean = True
            For Each item As Control In GroupBox1.Controls

                If item.Text = Nothing Then

                    ErrorProvider1.SetError(item, "enter a value")
                    PassedValidation = False

                Else

                    ErrorProvider1.SetError(item, "")

                End If

            Next
            If PassedValidation Then
            
                     ''code to save
                    ''

                    'Me.Validate()
                    'Me.UserprofileBindingSource.EndEdit()
                    'Me.TableAdapterManager.UpdateAll(Me.SampleDataSet)
                    MessageBox.Show("Saved", "System")
            End If


        Catch ex As Exception

        End Try

    End Sub


This way all the validation runs, then if any one or more fail to validate then the save function will not execute.

Your code was currently going to try and save each time a control passed validation.
 
Share this answer
 
Comments
akosisugar 20-May-13 12:49pm    
sir.. thank you! its perfect. its working now. i clearly understand what you you saying.. thank you very much sir.!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900