Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my function:
VB
Private Function validatingControls() As Boolean
       Dim cmbs() As ComboBox = {cmbEmployee, cmbParty, cmbSoftwares, cmbStatus}
       Dim rtb() As RichTextBox = {rtbTextArea}

       Dim empytCB As ComboBox = cmbs.Where(Function(f) f.SelectedIndex = -1).FirstOrDefault
       Dim emptyRB As RichTextBox = rtb.Where(Function(f) f.Text = "").FirstOrDefault

       If empytCB IsNot Nothing Then
           MessageBox.Show("please select a value in dropdown" & empytCB.Name)
           Return False

       ElseIf emptyRB IsNot Nothing Then
           MessageBox.Show("please write query")
           Return False

       Else

           Return True
       End If
   End Function


I've just called this in my button click event, it is automatically saving the data.
This is my button click event:

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click

       validatingControls()

       Dim conn2 As String = Nothing
       'Dim con As SqlConnection
       'Dim cmd As SqlCommand
       Dim adp As SqlDataAdapter = New SqlDataAdapter()
       Dim ds As DataSet = New DataSet()
       Dim i As Integer = 0
       Dim sql As String = Nothing
       Try

           con.Open()
           Dim cmd As SqlCommand = New SqlCommand("Insert into quaries values('" & cmbEmployee.Text & "','" & cmbParty.Text & "','" & cmbSoftwares.Text & "','" & cmbStatus.Text & "','" & rtbTextArea.Text & "','" & DateTimePicker1.Value & "')", con)
           cmd.ExecuteNonQuery()
           MsgBox("successfully saved")
           clearAllFields()
           con.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try

   End Sub
Posted
Updated 28-Jan-15 23:35pm
v2

Your call to validatingControls() in Button1_Click is ignoring the return value so you are saving stuff regardless of whether or not the validations passed

Try
If Not validatingControls() Then
      Exit Sub
End If
 
Share this answer
 
Comments
Member 11390168 31-Jan-15 2:30am    
thank you it is working
try..
VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
 
        If (validatingControls()) then
 
        Dim conn2 As String = Nothing
        'Dim con As SqlConnection
        'Dim cmd As SqlCommand
        Dim adp As SqlDataAdapter = New SqlDataAdapter()
        Dim ds As DataSet = New DataSet()
        Dim i As Integer = 0
        Dim sql As String = Nothing
        Try
 
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand("Insert into quaries values('" & cmbEmployee.Text & "','" & cmbParty.Text & "','" & cmbSoftwares.Text & "','" & cmbStatus.Text & "','" & rtbTextArea.Text & "','" & DateTimePicker1.Value & "')", con)
            cmd.ExecuteNonQuery()
            MsgBox("successfully saved")
            clearAllFields()
            con.Close()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
Else 
    return
 End If
    End Sub

N:B i'm Not good in Vb.net :)
 
Share this answer
 
Comments
Member 11390168 31-Jan-15 2:30am    
thank you so much it is working

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