Pramod V

Pramod V

  • NA
  • 5
  • 996

MemoryStream Exception: System.ObjectDispose Exception

Mar 28 2015 9:16 AM
Hi All, I have the following code for writing to PDF file; what it does is it reads html template from Settings file and replaces those values with DB field values and reneders html as PDF using pd4ml.dll libraries. What I am unable to understand is, why memory stream is closed even though I didn't close it explicitly. There are 5(Out of 17) overloaded methods for render method and currently I tested in all of them; I get same exception. Please help me understand this behaviour.
public static void template_Creation() {     String template = Resource_Bundle.Default.Template;     template = template.Replace("#heading#", "test");      template = template.Replace("#job_sheet_details#", "test");     template = template.Replace("#total_price#", "test");      String path = null;      SaveFileDialog saveFileDialog = new SaveFileDialog();     saveFileDialog.InitialDirectory = "%UserProfile%/Desktop";     saveFileDialog.Filter = "pdf files (*.pdf)|*.pdf";     saveFileDialog.FilterIndex = 1;     saveFileDialog.RestoreDirectory = true;      PD4ML PDFcreator = new PD4ML();     PDFcreator.PageSize = PD4Constants.A5;     byte[] bytes = Encoding.UTF8.GetBytes(template);     MemoryStream ms = new MemoryStream(bytes);                  StreamReader reader = new StreamReader(ms);     Stream ostream = ms;      try     {         if (saveFileDialog.ShowDialog() == DialogResult.OK)         {             path = saveFileDialog.FileName;             StreamWriter writer = new StreamWriter(path);             //PDFcreator.generatePdfa(true);             //PDFcreator.render(ms, writer);             PDFcreator.render(reader, ostream);             //PDFcreator.render(reader, writer);             //PDFcreator.generatePdfa(true);             //PDFcreator.clearCache();             ms.Close();             reader.Close();             writer.Close();         }     }     catch (Exception ex)     {         MessageBox.Show(ex.StackTrace);     } }


Answers (4)