title: Endless loop crashing VShost
body: <p>Currently using VS2013,and what I am trying to do is to update the last entry on a table while a certain condition is true, however, my solution to this problem is not working properly since it crashes VShost. I decided maybe some experienced eyes would see the problem in a better way and help me out, the code is this:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      MessageBox.Show("A apagar dados...");
      bool check = true;
      do
      {
          string connectionString;
          var path = @"C:\Users\Administrator\Desktop\connstring.txt";
          using (StreamReader sr = new StreamReader(path))
          {
              connectionString = sr.ReadLine();
          }
  
          var connection = new SqlConnection(connectionString);
  
          string queryString = string.Empty;
          using (SqlConnection deleteconnection = new SqlConnection(connectionString))
          {
              deleteconnection.Open();
              connection.Open();
              queryString = "DELETE FROM wgcdoccab WHERE numdoc = (SELECT MAX(numdoc) FROM WGCDOCCAB WHERE serie ='1' and tipodoc ='FSS' and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),dateadd(dd, -1, getdate()),120))";
              SqlCommand command = new SqlCommand(queryString, connection);
              command.ExecuteNonQuery();
              connection.Close();
          }
          using (SqlConnection deleteconnection = new SqlConnection(connectionString))
          {
              connection.Open();
              deleteconnection.Open();
  
              queryString = "SELECT max(numdoc) FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS' and contribuinte ='999999990' and datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())), 120))";
              using (SqlCommand command = new SqlCommand(queryString, connection))
              using (SqlDataReader reader = command.ExecuteReader())
              {
                  if (reader.HasRows)
                  {
                      check = true;
                  }
                  else
                  {
                      check = false;
                      MessageBox.Show("Dados Apagados com sucesso");
                  }
                  command.Connection.Close();
              }
          }
      }
      while (check);
  </code></pre>
  
  <p>After the button is clicked, the connections is opened, the query runs and supposedly updates the entries on the table one by one as they meet the condition and then stops when its the condition is false, however the VShost keeps crashing and I have no idea how to fix this, as I said maybe some more experienced yes could help me and give me an explanation on how to work the problem.</p>
  
  <p>EDIT: Thank you all for the help, i also stumbled upon this on the microsoft website, its a more efficient way to do it: </p>
  
  <blockquote>
    <p><a href="https://support.microsoft.com/en-us/kb/111401" rel="nofollow">https://support.microsoft.com/en-us/kb/111401</a></p>
  </blockquote>
  
  <p>Again thank you for the help you all gave!</p>