title: A new connection was rejected because the maximum number of connections has been reached
body: <p>I wrote a code in C# that get data from local database, manipulate and then sync it with a third party.</p>
  
  <p>I open connection in start of my application and then close it when all of my calls are ended(as I need an Open connection in my loop). I am using datareaders and datatable and properly disposing these after use.</p>
  
  <pre><code>using (DataTable dataTableLocation = new DataTable()) //this is inside a loop
  {
      using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand(queryLocation, conn))
      {
         cmd.CommandType = System.Data.CommandType.StoredProcedure;
         cmd.Parameters.Add(new SqlParameter("@id", id));
  
         // Use the above SqlCommand object to create a SqlDataReader object.
         using (SqlDataReader queryCommandReader3 = cmd.ExecuteReader())
         {
              // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
              dataTableLocation.Load(queryCommandReader3);
         }
  
      }
      //Data manipulation and then sync
  }
  </code></pre>
  
  <p>I have set Max Pool Size to 800. After 4 hours my console app get crashed and when I looked in windows event, it says </p>
  
  <blockquote>
    <p>A new connection was rejected because the maximum number of connections on session ID 63 has been reached. Close an existing connection on this session and retry</p>
  </blockquote>
  
  <p>While calls are made I constantly monitored database to see active connections, it shows 7-8</p>
  
  <pre><code>select * from sys.dm_os_performance_counters where counter_name ='User Connections'
  </code></pre>
  
  <p>Do I have to dispose,close connection and then recreate it again in a every loop?</p>
  
  <p>Code for DB connection </p>
  
  <pre><code>string connectionString = Properties.Settings.Default.connectionString;
  SqlConnection  conn = new SqlConnection(connectionString);
  </code></pre>