title: Internal Server Error - 500 while making simultaneous AJAX requests in ASP.NET MVC4
body: <pre><code>function InitializeCharts() {
   for (var i = 0; i &lt; arguments.length; i++) {
       compName = arguments[i];
       $.ajax({
           async:true,
           timeout: 60000,
           dataType: "json",
           url: "/SalesDashboard/GetChartData",
           data: { company: compName, from: startDate, to: endDate },
           success: (function () {
               var cmp = compName;
               return function (data) {
                   ChartData[cmp] = data;
                   PrepareChart(data, cmp);
               };
           })(),
           error: function (xhr, ajaxOptions, thrownError) {
               console.log(xhr.status+': '+xhr.statusText);                
               console.log(thrownError);
           }
       });
   }
  }
  </code></pre>
  
  <p>I'm using the above function at Client Side to make AJAX requests and get some data. It's working fine when I'm calling the function like this:</p>
  
  <pre><code>InitializeCharts("ABC"); // A Single parameter
  </code></pre>
  
  <p>It works fine for a single request but when I'm passing multiple parameters like below, it creates multiple simultaneous requests and gives me "<strong>Internal Server Error -500</strong>" Error.</p>
  
  <pre><code>InitializeCharts("ABC", "BBC", "CCC", "DDD", "EEE", "ACD", "CBD", "CDS", "ADB"); // Multiple parameters
  </code></pre>
  
  <p>It gives me the error for few random companies, sometimes for two, sometimes for three or four.
  I tried increasing the timeout in AJAX request to 60000 but it didn't work. I also went through IIS Server and increased the connection timeout to 3 minutes but still getting the same response. I know it even doesn't take a single minute to get the data back to the client. If I'm setting the "<strong>async</strong>" value in ajax request to false, it works but blocks the UI until the data returned for all companies.</p>
  
  <p>Below is my Server Side Code:</p>
  
  <pre><code>static int hitCount = 0;
  private string ConnectionString = "server=localhost;database={0};uid=test;password=test;pooling=false;";
  
  private string QueryString_Daily = "SELECT Day(mergefiledate) AS `Date`, COUNT(order_no) AS `Count` FROM orders_new WHERE DATE(mergefiledate)&gt;='{0}' and Date(mergefiledate)&lt;='{1}' GROUP BY DATE(mergefiledate);";
  
  public Task&lt;string&gt; GetChartData(string company, string from, string to)
      {
          hitCount++;
          return Task.Factory.StartNew(() =&gt; GetData(company, from, to));
      }
  
  private string GetData(string company, string from, string to)
      {            
           string QuerySTR = string.Empty;
           QuerySTR = string.Format(QueryString_Daily, startDt.ToString("yyyy-MM-dd"), endDt.ToString("yyyy-MM-dd"));
           MySqlConnection mycon = new MySqlConnection(string.Format(ConnectionString, company));
          MySqlCommand cmd = new MySqlCommand(QuerySTR, mycon);
          cmd.CommandTimeout = 0;
          DataTable dt = new DataTable();
          try
          {
              mycon.Open();
          }
          catch (Exception ex)
          {
              Response.Write("&lt;script&gt;alert('Database connection error, please try again. ERROR_DETAILS: '" + ex.Message + "');&lt;/script&gt;");
              return null;
          }
  
          if (mycon.State == ConnectionState.Open)
          {
  
              MySqlDataAdapter da = new MySqlDataAdapter(cmd);
              da.Fill(dt);
  
          }
  
          if (mycon.State == ConnectionState.Open)
          {
              try
              {
                  mycon.Close();
              }
              catch { }
          }
  
          if (dt.Rows.Count &gt; 0)
          {
      var result = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
               return result;
       }
      else
       {
              return null;
           }
  
      }
  </code></pre>
  
  <p>I've been trying to figure out the solution by Googling, digging on stackoverflow.com ... etc but no luck yet.
  Help is highly appreciated, Thanks a lot in advance.</p>