title: Run stored procedure asynchronously
body: <p>I've been playing around with the code below.  When the button is clicked, the idea is that a stored procedure runs and updates a table with a random number of dummy records (for now, whilst I'm playing, anyway).</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
          {
              SqlConnection conn = new SqlConnection("Data Source=MATT\\RICHARDSON2008R2;Initial Catalog=Minerva;User ID=User;Password=password; Asynchronous Processing=True");
              SqlCommand cmd = new SqlCommand("exec UpdateRandomData '" + UpdateID.Text + "'",conn);
  
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
          }
  </code></pre>
  
  <p>The stored procedure I've written adds 100000 rows using a loop (to simulate a procedure that can take some time to run):-</p>
  
  <pre><code>ALTER procedure [dbo].[UpdateRandomData]
      @updateID varchar(50)
  as
  declare @count int = 1;
  declare @theDate datetime = getdate();
  declare @total int  =FLOOR(rand() * 100000);
  
  while @count &lt; 100001
  begin
      insert into RandomData (randnumber, updateID, updatedDate)
      values 
      (FLOOR(rand() * 1001), @updateID, @theDate)
      set @count += 1;
  end
  GO
  </code></pre>
  
  <p>When I run the above C# code I get a timeout before the stored procedure finishes running, so I tried  <code>cmd.ExecuteNonQueryAsync();</code> instead:-</p>
  
  <pre><code>conn.Open();
  cmd.ExecuteNonQueryAsync();
  conn.Close();
  </code></pre>
  
  <p>The problem with this is that it doesn't seem to work the way I expect and I only ever add one row to the table from my stored procedure.</p>
  
  <p>Can anybody point me in the right direction why this isn't working as I want?</p>