title: Stream overwritten in async operation?
body: <p>The following is a snippet of my code, which sometimes throws an error due to part of the stream is missing. While debugging I can see that the stream has already been replaced by a new stream before it has been processed and therefore only part of the stream reaches the next step, I think. I'm new to asyncrounus operations and I might use the wrong terminology. As I have understood async is not the same as asyncronus. </p>
  
  <p>The error is an nullreference exception error and is thrown on the line datetime? dttest... When looking into the xe it only shows the first part of the xmldoc. The xml does exist on the website hosting it and is correct including all informationelements on the webpage itself. It can not be excluded that it might be an error in the stream or that the stream gets overwritten before it has been processed. Thats why the snippet includes a bigger part of the code.</p>
  
  <pre><code>foreach (DataRow row in dt.Rows)
              {
                  try
                  {
                      await Task.Delay(100);
                      url = row["href"].ToString();
                      k = int.Parse(row["K"].ToString());
  
                      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                      NetworkCredential nc = new NetworkCredential(customerNC, "");
                      request.Credentials = nc;
                      request.Method = "GET";
                      request.Timeout = 20000;
                      request.Proxy = null;
                      request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
                      var response = await Policy
                          .Handle&lt;WebException&gt;()
                          .WaitAndRetry(3, retryAttempt =&gt; TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (Exception, TimeSpan, context) =&gt;
                          {
                              MessageBox.Show(Exception.Message);
                          })
                          .Execute(async () =&gt; (HttpWebResponse)await Task.Factory.FromAsync&lt;WebResponse&gt;(request.BeginGetResponse, request.EndGetResponse, null));
  
                      Stream stream = response.GetResponseStream();
                      StreamReader strReader = new StreamReader(stream);
                      var text = await strReader.ReadToEndAsync();
  
                      XNamespace xlink = "http://www.w3.org/1999/xlink";
                      XDocument doc = XDocument.Parse(text);
  
                      xml = text;
  
                      using (SqlConnection connection = new SqlConnection(connectionString))
                      {
                          await connection.OpenAsync();
  
                              if (k == 1)
                              {
                                  kid = int.Parse(row["kid"].ToString());
                                  string sql = @"     UPDATE dbo." + clientTableID + @"Produkter
                                                  SET ProdTrue = 0,
                                                  StockTrue = 0,
                                                  OptTrue = 0,
                                                  CombTrue = 0
                                                  WHERE DB_ID = @1
                                                  "
                                                      ;
  
                                  using (SqlCommand cmd2 = new SqlCommand(sql, connection))
                                  {
                                      cmd2.Parameters.AddWithValue("@1", kid);
                                      await cmd2.ExecuteNonQueryAsync().ConfigureAwait(false);
                                  }
                              }
  
                              foreach (XElement xe in doc.Descendants("product"))
                              {
                                  DateTime dt3 = DateTime.Now;
                                  SqlDataReader dr;
                                  int id = int.Parse(xe.Element("id").Value);
                                  SqlCommand sqlQuery = new SqlCommand(@"SELECT db_id, lastupd, prodtrue FROM " + clientTableID + @"produkter WHERE EXISTS (SELECT DB_ID WHERE DB_ID = " + id + ")", connection);
                                  DateTime dt2 = DateTime.Now;
                                  int dbid = 0;
                                  int ProdTrue = 0;
  
                                  DateTime? dttest = Convert.ToDateTime(xe.Element("date_upd").Value);
                                  if (dttest != null)
                                  {
                                      dt3 = DateTime.Parse(dttest.ToString());
                                  }
                                  else
                                  {
                                      dt3 = DateTime.Now;
                                  }
  }
  </code></pre>