title: Generate different pdf files for each row in data table and send Email Attachment
body: <p>I need to have a functionality that on click of a button i need to generate pdf file for each record in the data table using C#.net</p>
  
  <p>I require to generate PDF for each and then Email attachment that PDF to each person. the data for PDF is fetched into a data table for all students.</p>
  
  <p>I just need a functionality that i can generate individual pdf for each row from data table and send email attachment.</p>
  
  <p>Can anyone please help me  and tell me any other packages are available in C#.net.</p>
  
  <p>I have tried this way but does not work and this working for one row in the data table after that its not returning next loop to generate pdf, please look at the following code.</p>
  
  <pre><code>StringBuilder sb = new StringBuilder();
  string companyName = "ASPSnippets";
  int orderNo = 2303;
  protected void BTNsubmint_Click(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection("server=ServerName; database=Dbname; Integrated Security=True");
      SqlDataAdapter da = new SqlDataAdapter("select * from productss", con);
      DataTable dt = new DataTable();          
      da.Fill(dt);
      using (StringWriter sw = new StringWriter())
      {
          using (HtmlTextWriter hw = new HtmlTextWriter(sw))
          {
  
              //Generate Invoice (Bill) Header.
              sb.Append("&lt;table width='100%' cellspacing='0' cellpadding='2'&gt;");
              sb.Append("&lt;tr&gt;&lt;td align='center' style='background-color: #18B5F0' colspan = '2'&gt;&lt;b&gt;Order Sheet&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;");
              sb.Append("&lt;tr&gt;&lt;td colspan = '2'&gt;&lt;/td&gt;&lt;/tr&gt;");
              sb.Append("&lt;tr&gt;&lt;td&gt;&lt;b&gt;Order No: &lt;/b&gt;");
              sb.Append(orderNo);
              sb.Append("&lt;/td&gt;&lt;td align = 'right'&gt;&lt;b&gt;Date: &lt;/b&gt;");
              sb.Append(DateTime.Now);
              sb.Append(" &lt;/td&gt;&lt;/tr&gt;");
              sb.Append("&lt;tr&gt;&lt;td colspan = '2'&gt;&lt;b&gt;Company Name: &lt;/b&gt;");
              sb.Append(companyName);
              sb.Append("&lt;/td&gt;&lt;/tr&gt;");
              sb.Append("&lt;/table&gt;");
              sb.Append("&lt;br /&gt;");
  
              //Generate Invoice (Bill) Items Grid.
              sb.Append("&lt;table border = '1'&gt;");
              sb.Append("&lt;tr&gt;");
              foreach (DataColumn column in dt.Columns)
              {
                  sb.Append("&lt;th&gt;");
                  sb.Append(column.ColumnName);
                  sb.Append("&lt;/th&gt;");
              }
              sb.Append("&lt;/tr&gt;");
              foreach (DataRow row in dt.Rows)
              {
                  sb.Append("&lt;tr&gt;");
                  foreach (DataColumn column in dt.Columns)
                  {
                      sb.Append("&lt;td&gt;");
                      sb.Append(row[column]);
                      sb.Append("&lt;/td&gt;");
                  }
  
                  sb.Append("&lt;/tr&gt;");
                  sb.Append("&lt;/table&gt;");
  
                  StringReader sr = new StringReader(sb.ToString());
                  Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                  HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                  PdfWriter writer = PdfWriter.GetInstance(pdfDoc,                               Response.OutputStream);
                  pdfDoc.Open();
                  htmlparser.Parse(sr);
                  pdfDoc.Close();
                  Response.ContentType = "application/pdf";
                  Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + orderNo + ".pdf");
                  Response.Cache.SetCacheability(HttpCacheability.NoCache);
                  Response.Write(pdfDoc);
                  Response.End();
          }
      }
  }
  </code></pre>