title: Memorystream object being passed to method doesn't contain values
body: <p>I am using a memorystream object to write values from streamwriter object into memory. I have several methods which are for logging errors and values so I pass my memorystream object between them as a parameter. </p>
  
  <p>My problem is the memorystream doesn't contain values. See code below:</p>
  
  <pre><code>    #region csvlogging
      public static void initiateCsvLogging(SortedList&lt;int, string&gt; logList, SortedList&lt;int, string&gt; errorList) // Handles csv upload logging
      {
          logMsgError(errorList, logList);
      }
  
      public static void logMsgError(SortedList&lt;int, string&gt; errorList, SortedList&lt;int, string&gt; logList)
      {
          using (MemoryStream ms = new MemoryStream())
          {
             StreamWriter sw = new StreamWriter(ms);
              try
              {
                  sw.WriteLine("Errors:");
                  foreach (KeyValuePair&lt;int, string&gt; k in errorList)
                  {
  
                      if (k.Value == null)
                      {
                          sw.WriteLine("No errors reported.");
                      }
                      else
                      {
                          sw.WriteLine(k.Key + "," + k.Value);
                      }
                  }
              }
              catch (Exception ex)
              {
  
              }
              finally
              {
                  sw.WriteLine("");
              }
  
              logMsg(logList, ms);
  
          }
      } // Handles data upload error logging for csv
  
      public static MemoryStream logMsg(SortedList&lt;int, string&gt; logList, MemoryStream ms)
      {
          string DateNow = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
          string FileName = "log " + DateNow + ".csv";
          char[] delimiterChars = { ',' };
  
          try
          {
              // Write values to textfile and save to Log folder
              using (StreamWriter sw = new StreamWriter(ms))
              {
                  if (logList.Keys.Count == 0)
                  {
                      sw.WriteLine("No new users added to Active Directory.");
                  }
                  else  // If keys &gt; 0 then new users have been added to AD
                  {
                      sw.WriteLine("Username" + "," + "Password" + "," + "Company" + "," + "Email");
                      foreach (KeyValuePair&lt;int, string&gt; k in logList)
                      {
                          string[] values = k.Value.Split(delimiterChars);
                          sw.WriteLine(values[0] + "," + values[1] + "," + values[2] + "," + values[3]);
                      }
                  }
  
                  try
                  {
                      sw.Flush();
                      sendLogByEmail(new MemoryStream(ms.ToArray()), FileName);
                  }
                  catch (Exception ex)
                  {
  
                  }
              }
          }
  
          catch (ThreadAbortException ex)
          {
  
          }
  
          finally
          {
  
          }
          return ms;
      } // Handles logging for csv
  
      public static void sendLogByEmail(MemoryStream ms, string error, int count)
      {
          //Send mail by attachment code
          SmtpClient smtpclient = new SmtpClient();
          //smtpclient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
          //smtpclient.UseDefaultCredentials = true;
          smtpclient.Host = "ldnmail";
  
  
          MailMessage message = new MailMessage("nick.gowdy@orcinternational.co.uk", "nick.gowdy@orcinternational.co.uk");
          System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
          System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
          attach.ContentDisposition.FileName = error;
  
          message.Attachments.Add(attach);
          smtpclient.Send(message);
  
      }
      #endregion
  </code></pre>
  
  <p>Because I am not using the USING keyword do I have to write some more code for the memorystream object?</p>