Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, im having trouble with my code here. I want to show messagebox if the user missed a textbox field blank.

Here's my code:

VB
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        MySqlConn = New MySqlConnection
        MySqlConn.ConnectionString =
            "server=localhost;userid=root;password=ianp;database=database"
        Dim READER As MySqlDataReader

        

        Try
            MySqlConn.Open()
            Dim Query As String
            Query = "insert into database.appointment (AppointmentID, FirstName, MiddleName, LastName, Date, Time) values ('" & txtAppointmentID.Text & "','" & txtFNAppointment.Text & "', '" & txtMNAppointment.Text & "', '" & txtLNAppointment.Text & "', '" & dtDate.Text & "', '" & dtTime.Text & "')"
            COMMAND = New MySqlCommand(Query, MySqlConn)
            READER = COMMAND.ExecuteReader

            If txtFNAppointment.Text & txtMNAppointment.Text & txtLNAppointment.Text ="" Then
                MessageBox.Show("Please fill-up all the required fields !")

            Else
                MessageBox.Show("Successfully Saved Record")
autogenerate_id()
            End If

            MySqlConn.Close()

        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            MySqlConn.Dispose()

        End Try


I have problems: When i left a field blank. It Shows the MessageBox "Please fill-up all the required fields !" but it saves in the mysql database. I want for it to abort the save until all the fields are filled-up. Any help will be appreciated. Thanks
Posted
Updated 13-Sep-15 18:10pm
v2

It's best to check each field separately. Also you need to exit the sub. So instead of
VB
If txtFNAppointment.Text & txtMNAppointment.Text & txtLNAppointment.Text ="" Then
MessageBox.Show("Please fill-up all the required fields !")

try
VB
If txtFNAppointment.Text = "" Or txtMNAppointment.Text = "" Or txtLNAppointment.Text ="" Then
   MessageBox.Show("Please fill-up all the required fields !")
   Return
End If


And what comes to your query, never concatenate values directly into the SQL statement. This leaves you open to SQL injections. Instead, use SqlParameter[^]. For more discussion, see Properly executing database operations, version 3, parameters[^]
 
Share this answer
 
v3
Comments
Member 11981458 14-Sep-15 0:34am    
I tried both of them but even if you leave the fields blank, it always saves in the database. I would love for it to just show the messagebox. It will only save if all of the fields are filled-up
validate and show message if any filed is missing, otherwise do the insert
VB
If String.IsNullOrWhiteSpace(txtFNAppointment.Text) or String.IsNullOrWhiteSpace(txtMNAppointment.Text) or String.IsNullOrWhiteSpace(txtLNAppointment.Text) Then
  MessageBox.Show("Please fill-up all the required fields !")
Else
   COMMAND = New MySqlCommand(Query, MySqlConn)
   COMMAND.ExcuteNonQuery()
End If
 
Share this answer
 
Comments
Member 11981458 14-Sep-15 0:31am    
I tried both of them but even if you leave the fields blank, it always saves in the database. I would love for it to just show the messagebox. It will only save if all of the fields are filled-up
DamithSL 14-Sep-15 0:42am    
can you show the code you tried?
Member 11981458 14-Sep-15 0:48am    
I already got it to work sir Thanks a lot for your help. I just deleted the Code:
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
Seems that this is the code that saves the database and not basing it on the IfElse statement.
DamithSL 14-Sep-15 0:58am    
You are welcome!

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