I am using this code to mail a datatable in the form of a pdf. But on converting the data using html parser, it is showing the exception as Stack Empty. However I have done this before and it worked, I just cant put my finger on what am I missing now. Please help!!!!!
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
//sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
foreach (DataColumn column in dt.Columns)
{
//sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);//here is the error
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage(txtEmail.Text.Trim(), email.Trim());
mm.Subject = txtSubject.Text.Trim();
//mm.CC.Add = txtcc.Text.Trim();
mm.Body = txtBody.Text.Trim();
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = txtEmail.Text.Trim();
NetworkCred.Password = txtPassword.Text.Trim();
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
} }
sb.ToString()from html file or you build your html as hard coded in your program?