Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display a Winforms MySQL Connection Form on Error

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading