I created a Windows Forms application with C# that checks if a MySQL connection is open or closed. For each state, the text of a label should change.
My problem is: if the application keeps running and the connection is closed or terminated, the text of the label does not change.
I tried using the form_load
events, but it didn't work.
This is my code:
string connectionString = "Data Source=localhost;Initial Catalog=table;User ID=root;Password=";
public Form1()
{
InitializeComponent();
MySqlConnection cnn = new MySqlConnection(connectionString);
try
{
cnn.Open();
toolStripStatusLabel1.BackColor = System.Drawing.Color.Green;
toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
toolStripStatusLabel1.Text = "Connection Success";
}
catch (Exception ex)
{
toolStripStatusLabel1.BackColor = System.Drawing.Color.Red;
toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
toolStripStatusLabel1.Text = "Connection failed!";
}
finally
{
cnn.Close();
}
}