title: Why does my C# Winforms program with a timer get bogged down after a day?
body: <p>I have a program that updates two DataGridView objects every minute with a MySQL query from our internal orders database so we can keep track of older orders and make sure they're being handled.</p>
  
  <p>It works well, but if you leave the program on it starts to get slower and slower and you really notice it after a day or so. Restarting the program fixes this, but I'd really like to know why I can't just leave this running.</p>
  
  <p>Here is the timer code and the function it calls:</p>
  
  <pre><code>    private void timer1_Tick(object sender, EventArgs e)
      {
          UpdateOrderDisplay();
      }
  
          private void UpdateOrderDisplay()
      {
          grdOrderItems.Rows.Clear();
          grdHoldOrders.Rows.Clear();
          string strsql;
          string CustomerName;
          string MyConString = "SERVER=**********;" + "DATABASE=***********;" + "UID=************;" + "PASSWORD=***********;";
          using (MySqlConnection connection = new MySqlConnection(MyConString))
          {
              MySqlCommand command = connection.CreateCommand();
              MySqlDataReader Reader;
              strsql = "select * from orders where ship_reference=0 and OnHold =0 order by order_id asc";
              command.CommandText = strsql;
              connection.Open(); Reader = command.ExecuteReader();
              while (Reader.Read())
              {
                  if (Reader["payment_date"].ToString() != "")
                  {
                      if (Reader["custom"].ToString().Contains("~*"))
                      {
                          CustomerName = Reader["custom"].ToString().Substring(0, Reader["custom"].ToString().IndexOf("~"));
                      }
                      else
                      {
                          CustomerName = Reader["shipping_address_name"].ToString();
                      }
                      grdOrderItems.Invoke(new MethodInvoker(() =&gt; grdOrderItems.Rows.Add(Convert.ToDateTime(Reader["payment_date"].ToString().Substring(0, 21)).ToString(), Reader["txn_id"].ToString(), CustomerName, Reader["mc_gross"].ToString(), Reader["memo"].ToString(), Reader["order_id"].ToString())));
                  }
              }
              connection.Close();
              strsql = "select * from orders where OnHold =1 order by order_id asc";
              command.CommandText = strsql;
              connection.Open(); Reader = command.ExecuteReader();
              while (Reader.Read())
              {
                  if (Reader["payment_date"].ToString() != "")
                  {
                      if (Reader["custom"].ToString().Contains("~*"))
                      {
                          CustomerName = Reader["custom"].ToString().Substring(0, Reader["custom"].ToString().IndexOf("~"));
                      }
                      else
                      {
                          CustomerName = Reader["shipping_address_name"].ToString();
                      }
                      grdHoldOrders.Invoke(new MethodInvoker(() =&gt; grdHoldOrders.Rows.Add(Convert.ToDateTime(Reader["payment_date"].ToString().Substring(0, 21)).ToString(), Reader["txn_id"].ToString(), CustomerName, Reader["mc_gross"].ToString(), Reader["memo"].ToString(), Reader["order_id"].ToString(),Reader["Hold_Review_Date"].ToString().Substring(0,Reader["Hold_Review_Date"].ToString().IndexOf(" ")),Reader["payer_email"].ToString())));
                  }
              }
  
  
  
  
  
              connection.Close();
          }
  
      }
  </code></pre>
  
  <p>Also, in case it's relevant, here is some code I use to color code the lines of the DataGridView object so we can easily tell which order is older:</p>
  
  <pre><code>    private void grdOrderItems_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
      {
          grdOrderItems.RowPrePaint += new System.Windows.Forms.DataGridViewRowPrePaintEventHandler(this.grdOrderItems1_RowPrePaint);
  
      }
      private void grdOrderItems1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
      {
          if (e.RowIndex &lt;= grdOrderItems.Rows.Count - 1)
          {
              string StringNow = DateTime.Now.ToString();
              string NowTime = StringNow.Substring(StringNow.IndexOf(" ")+1, StringNow.Length- StringNow.IndexOf(" ")-1);
              string OrderDateTime = grdOrderItems.Rows[e.RowIndex].Cells[0].Value.ToString().Substring(0, grdOrderItems.Rows[e.RowIndex].Cells[0].Value.ToString().IndexOf(" ")+1) + NowTime;
              if ((Convert.ToDateTime(StringNow) - Convert.ToDateTime(OrderDateTime)).Days &gt; 2)
              {
                  grdOrderItems.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Tomato;
              }
              else
              {
                  if ((Convert.ToDateTime(StringNow) - Convert.ToDateTime(OrderDateTime)).Days &gt; 1)
                  {
                      grdOrderItems.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                  }
                  else
                  {
                      if ((Convert.ToDateTime(StringNow) - Convert.ToDateTime(OrderDateTime)).Days &gt; 0)
                      {
                          grdOrderItems.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Tan;
                      }
  
                  }
              }
          }
      }
  </code></pre>