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) ; } }Loading
Pramod VPosted Mar 29, 2015, 1:27 AM
I have modified my code in the following way: But I still get the same error and I think if a Stream is opened and we put some bytes of data; and again open another stream would previous stream automatically closed?
Joginder BangerPosted Mar 28, 2015, 5:15 PM
I make a simple demo for better understand what is the meaning and advantage.
try
{
//place your above here
}
catch(Execption ex)
{
//show the error if have any type error occurred
}
finally() //not this block every time execute if your program is working fine or not.
{
disposed and closed all stream here..
}
Pramod VPosted Mar 28, 2015, 10:04 AM
Vithal WadjePosted Mar 28, 2015, 9:41 AM