error handling - how to use 'try catch finally' block to display message when mysql query is executed successfully -
i have been using 'try catch finally' in code c# windows form query mysql database. code works well: when catch block flags errors, message box display error messages.
in block, coded message box show when database updated.
if there no error message works well. success message shows.
but if there error, error message in catch block displayed, followed success message in block.
does know solution program display either error message or success message when mysql updated?
thanks,
peter
here code sample:
private void btnupdateemployeetable_click(object sender, eventargs e) //update record in employee table { string myconnection = @"server=localhost; database=shopdb; username=**; password=**; convert 0 datetime=true"; mysqlconnection connect = null;
try { connect = new mysqlconnection(myconnection); connect.open(); //open connection //this mysql command query db. //it uses prepared statements , placeholders faster, more secure processing. string updatequery = "update employee set emp_lname = @emplastname, emp_fname = @empfirstname emp_number = @empnum"; mysqlcommand cmdinsertemployeetodatabase = new mysqlcommand(updatequery, connect); cmdinsertemployeetodatabase.prepare(); //bind value 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 mysql command } catch (exception ex) { messagebox.show(ex.message + "\ndatabase not updated \n" + "please try again"); } { if (connect != null) { connect.close(); //close connection } messagebox.show("database update successful"); } }
you can move success code higher. if exception thrown before messagebox.show("database update successful");
line, never executed.
try { connect = new mysqlconnection(myconnection); connect.open(); //open connection //this mysql command query db. //it uses prepared statements , placeholders faster, more secure processing. string updatequery = "update employee set emp_lname = @emplastname, emp_fname = @empfirstname emp_number = @empnum"; mysqlcommand cmdinsertemployeetodatabase = new mysqlcommand(updatequery, connect); cmdinsertemployeetodatabase.prepare(); //bind value 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 mysql command messagebox.show("database update successful"); } catch (exception ex) { messagebox.show(ex.message + "\ndatabase not updated \n" + "please try again"); } { if (connect != null) { connect.close(); //close connection } }
Comments
Post a Comment