I am attempting to show a small windows form to the user when a mysql database connection error is encountered (in case the server's ip address changes, which is a rare but possible occurrence in this particular environment), as the form would allow the user to see the existing connection information and, if necessary, update that information so the connection can be re-attempted. Here is my current code:
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
MessageBox.Show("There was an error when connecting to the specified server.", "Connection Error", MessageBoxButtons.OK);
Form test = new ConnectionSettings();
test.Show();
throw;
}
However, for some reason the connection settings window pops up for just a moment before the application continues and (ultimately) displays an error. How can I get the application to pause and allow the user to update the values so the connection can be attempted a 2nd time?
(the "Connection Settings" form simply has 5 textboxes with labels, for ip address, port, database, username, and password, and the values are saved to the application settings...though I would love an alternative for the username and password, or all 5 values. My SQL code then pulls those values attempting to connect and send an SQL command, etc)
CodePudding user response:
This simplest way is to change this line:
test.Show();
to this
test.ShowDialog(this);
As @Charlieface mentioned in the comments, if you want conditional actions based on what the user does with the form, then you will need to either return a DialogResult
from the form or do some more complicated things in the form.