title: How to check SQL Server is availability using C# and ASP.NET 4.0
body: <p>I have client server architecture. I have server and 4 client machines, each client has SQL Server connected with LAN.</p>
  
  <p>I need to check the 4 client database was connected or not.</p>
  
  <p>In my web.config</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="DBConnection" 
           connectionString="Data Source=192.168.1.90,1433;Initial Catalog=POS;Integrated Security=False;User Id=sa;Password=sql2008;" 
           providerName="System.Data.SqlClient" /&gt;
      &lt;add name="DBConnection1" 
           connectionString="Data Source=192.168.1.91,1433;Initial Catalog=POS;Integrated Security=False;User Id=sa;Password=sql2008;Connection Timeout=5" 
           providerName="System.Data.SqlClient" /&gt;
      &lt;add name="DBConnection2" 
           connectionString="Data Source=192.168.1.92,1433;Initial Catalog=POS;Integrated Security=False;User Id=sa;Password=sql2008;Connection Timeout=5" 
           providerName="System.Data.SqlClient" /&gt;
      &lt;add name="DBConnection3" 
           connectionString="Data Source=192.168.1.93,1433;Initial Catalog=POS;Integrated Security=False;User Id=sa;Password=sql2008;Connection Timeout=5" 
           providerName="System.Data.SqlClient" /&gt;
      &lt;add name="DBConnection4" 
           connectionString="Data Source=192.168.1.94,1433;Initial Catalog=POS;Integrated Security=False;User Id=sa;Password=sql2008;Connection Timeout=5" 
           providerName="System.Data.SqlClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>In my ASP.NET code, I am using an ajax method inside the for loop</p>
  
  <pre><code>   &lt;script type="text/javascript"&gt;
       function CheckDB() {
  
           for (var i = 1; i &lt;= 4); i++) {
               var DBConnection = "DBConnection" + i;
               $.ajax({
                   type: 'POST',
                   url: 'StockIntegration.aspx/CheckDB',
                   data: "{DBConnection:" + JSON.stringify(DBConnection) + "}",
                   contentType: 'application/json; charset=utf-8',
                   dataType: 'json',
                   success: function (data) {
  
                       if (data.d == null || data.d == undefined) {
  
                       } else {
  
                           if (data.d == "1") {
                               $('[id$=Label1]').append("CONNECTED SYSTEM: " + DBConnection + "&lt;br/&gt;");
                           } else {
                               $('[id$=Label2]').append("DISCONNECTED SYSTEM: " + DBConnection + "&lt;br/&gt;");
                           }
                       }
                   },
                   error: function ()
                   { alert('there is some error'); }
               });
           }
       }
   &lt;/script&gt;
  </code></pre>
  
  <p>In my web service</p>
  
  <pre><code>[System.Web.Services.WebMethod]
  public static String CheckDB(String DBConnection)
  {
          try
          {
              SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[DBConnection].ConnectionString);
              SqlCommand cmd = new SqlCommand("select 1", con);
              con.Open();
              var rowCount = cmd.ExecuteScalar();
              con.Close();
              return rowCount.ToString();
          }
          catch
          {
              return "Fasle";
          }
  }
  </code></pre>
  
  <p>The loop will close if any of SQL Server instances is disconnected. How to achieve this? I need to show the list of connected and disconnected SQL Server instances.</p>