0

I have been using a 'try catch finally' in the code for a C# Windows form to query a MySQL database. The code works well: when the catch block flags errors, a message box will display error messages.

In the finally block, I coded a message box that will show when the database is successfully updated.

If there is no error message it all works well. The success message shows.

But if there is an error, the error message in the catch block is displayed, followed by the success message in the finally block.

Does anyone know a solution to get a program to display either an error message or a success message when mysql is updated?

Thanks,

Peter

Here is a code sample:

private void btnUpdateEmployeeTable_Click(object sender, EventArgs e) //Update record in Employee table { String myConnection = @"server=localhost; database=shopdb; username=**; password=**; convert zero datetime=True"; MySqlConnection Connect = null;

        try
        {
            Connect = new MySqlConnection(myConnection);
            Connect.Open(); //Open the connection

            //This is the mysql command that we will query into the db.
            //It uses Prepared statements and the Placeholders for faster, more secure processing.

            String updateQuery = "UPDATE employee SET emp_lName = @empLastName, emp_fName = @empFirstName WHERE emp_number = @empNum";

            MySqlCommand cmdInsertEmployeeToDataBase = new MySqlCommand(updateQuery, Connect);
            cmdInsertEmployeeToDataBase.Prepare();

            //Bind the value to the placeholder               

            cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empNum", this.txtEmployeeNo.Text);
            cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empLastName", this.txtEmployeeLastName.Text);
            cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empFirstName", this.txtEmployeeFirstName.Text);                

            cmdInsertEmployeeToDataBase.ExecuteNonQuery(); //Execute the mysql command 


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\nDatabase could not be updated \n" + "Please try again");

        }
        finally
        {
            if (Connect != null)
            {
                Connect.Close(); //Close the connection
            }


            MessageBox.Show("Database update successful");


        }
    }

1 Answers1

1

You can easily move the success code up higher. If an exception is thrown before the MessageBox.Show("Database update successful"); line, then it will never get executed.

     try
     {
        Connect = new MySqlConnection(myConnection);
        Connect.Open(); //Open the connection

        //This is the mysql command that we will query into the db.
        //It uses Prepared statements and the Placeholders for faster, more secure processing.

        String updateQuery = "UPDATE employee SET emp_lName = @empLastName, emp_fName = @empFirstName WHERE emp_number = @empNum";

        MySqlCommand cmdInsertEmployeeToDataBase = new MySqlCommand(updateQuery, Connect);
        cmdInsertEmployeeToDataBase.Prepare();

        //Bind the value to the placeholder               

        cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empNum", this.txtEmployeeNo.Text);
        cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empLastName", this.txtEmployeeLastName.Text);
        cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empFirstName", this.txtEmployeeFirstName.Text);

        cmdInsertEmployeeToDataBase.ExecuteNonQuery(); //Execute the mysql command 

        MessageBox.Show("Database update successful");

     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message + "\nDatabase could not be updated \n" + "Please try again");

     }
     finally
     {
        if (Connect != null)
        {
           Connect.Close(); //Close the connection
        }

     }
LVBen
  • 2,041
  • 13
  • 27