How to Export DataReader to Pdf in Asp.Net


In this Example we will See How to Create Pdf File from Repeater Control in dot net. For Export Data in Pdf file from Repeater we will use Attachment in Code behind. Here I have user table Controls to see data in tabular format and we will Export this Repeater Control in Pdf using Content type through Application. So let’s start to Create Table & Repeater and Export in Pdf file through Code behind in dot net.

Step 1: Create you Table in your Webpage and Design Like Below.

<div>
  <asp:Panel ID="Panel1" runat="server" Width="324px">
  <table border="1">
    <tr><td align="center" style="background-color: #6C6C6C; line-height: 200%; color: White; width: 100px; font-size: 10pt; font-family: Arial">
    Name</td>
    <td align="center" style="background-color: #6C6C6C; line-height: 200%; color: White;
    width: 100px; font-size: 10pt; font-family: Arial">
    City</td><td align="center" style="background-color: #6C6C6C; line-height: 200%; color: White; width: 100px; font-size: 10pt; font-family: Arial">
    Country</td></tr>
    <asp:Repeater ID="Repeater1" runat="server">
      <ItemTemplate>
        <tr>
          <td align="center" style="background-color: #eee; color: black; font-size: 10pt; width: 100px; font-family: Arial">
          <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label></td>
          <td align="center" style="background-color: #eee; color: black; font-size: 10pt; width: 100px; font-family: Arial">
          <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
          </td><td align="center" style="background-color: #eee; color: black; font-size: 10pt; width: 100px; font-family: Arial">
          <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label></td></tr>
      </ItemTemplate>
    </asp:Repeater>
  </table>
  </asp:Panel>&nbsp;
  <asp:Button ID="btnExport" runat="server" Text="Export"             OnClick="btnExport_Click" Height="29px" Width="99px" />
</div>

Step 2: Now go to Code behind and we will code for Bind Data in table. Now Create ConnectionString for getting data from Database here we will create ConnectionString in Web.Config file and use in Code behind.

<connectionStrings>
    <add name="DBCS" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Step 3: Now in Code behind we will retrieve ConnectionString like below.

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

Step 4: Now Create Method for getting data from database and bind in Repeater Control and Call Created method in Page_load. Or else you can directly bind repeater control in page load method.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmd = new SqlCommand("Select * from customers", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Repeater1.DataSource = dt;
                    Repeater1.DataBind();
                }
            }
         }
Step 5: Now Code for Create Pdf file on button click event.

protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=ReaderDataExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Panel1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
Step 6: Run Application and see output. 
 

Related Posts

Previous
Next Post »

Thanks for comments.....