title: How to Check connection status at run time using C#.net (winforms)
body: <p>I have client server application (Lan base). Initially the client application successfully connects to server but later at run time the connection is being lost due to some reason (e.g server machine shutdown ). This is triggering an error something like <code>UNABLE TO CONNECT TO HOST</code>. How can i check the status of connection at run time?</p>
  
  <p>The code is as follows</p>
  
  <p>Connection class</p>
  
  <pre><code>using MySql.Data.MySqlClient;  
  namespace Connection_Checker
  {
   public class Connection
   {
     public static MySqlConnection cnn = new MySqlConnection("Server=localhost;Database=arc;User Id=root;Password=root;");
   }
  }
  </code></pre>
  
  <p>Form</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
  {
    Connection.cnn.Open();
  }
  
  //and timer
  private void timer1_Tick(object sender, EventArgs e)
  {
    if (Connection.cnn.State == ConnectionState.Open)
    {
      this.Text = "Connected";
    }
    else
    {
      this.Text = "Disconnected";
      return; 
    }
  }
  </code></pre>
  
  <p>my timer is enable = true</p>