Friday, 5 July 2013

Simple Date Report and Export Gridview to PDF

Simple Date Report and Export Gridview to PDF
For PDF you have to Downalod
For exporting the data, I am using the iTextSharp (third party dll) in this post. Download the iTextSharp .
iTextSharp .DLL files and Add in your Project
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Report.aspx.cs" Inherits="Report" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
   <table class="style1" align="center">
                    <td bgcolor="#99CCFF" colspan="2" style="text-align: center">
                        S<b>how Reports on User Registered </b></td>
                        <asp:Label ID="lblstartdate" runat="server" Text="Registration Date"></asp:Label>
                        <asp:TextBox ID="txtstartingdate" runat="server"></asp:TextBox>
                        <br />
                        <asp:Calendar ID="Calendar1" runat="server"
                        <asp:Label ID="lblendingdate" runat="server" Text="Ending Date"></asp:Label>
                        <asp:TextBox ID="txtendingdate" runat="server"></asp:TextBox>
                        <asp:Button ID="btnfilter" runat="server" Text="Show  Registered Users" Width="166px"
                            onclick="btnfilter_Click" />

                <td><asp:ImageButton ID="btnPDF" runat="server" ImageUrl="~/Images/pdf.jpg"   Width="32px"
Height="32px" onclick="btnPDF_Click"/></td>

                <td><asp:GridView ID="GVMec" runat="server">



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Data;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

using System.IO;

public partial class Report : System.Web.UI.Page
    string Str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)


    protected void btnfilter_Click(object sender, EventArgs e)
          SqlConnection conn= new SqlConnection(Str);
                SqlCommand cmd = new SqlCommand();
                // cmd.Parameters.Add(new SqlParameter("@Date1", SqlDbType.DateTime)).Value = txtstartingdate ;
                SqlDataAdapter sda = new SqlDataAdapter("Select * From Tbl_User_Registration Where U_Rdate= '" + txtstartingdate.Text + "' ", conn);
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                //Create a DataTable to hold the query results.
                DataTable dTable = new DataTable();
                //Fill the DataTable.
                if (dTable.Rows.Count > 0)
                    GVMec.DataSource = dTable;
                    Response.Write("<script>alert('No Recodrd Found')</script>");


    public override void VerifyRenderingInServerForm(Control control)
        /* Verifies that the control is rendered */
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        txtstartingdate.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");

// Below is Code for Exporting Gridview to PDF
    protected void btnPDF_Click(object sender, ImageClickEventArgs e)
        Response.ContentType = "application/pdf";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //Set AllowPaginf false to export the full data
        GVMec.AllowPaging = false;
       // GVMec.DataBind();
        //Start the rendering of control here
        foreach (GridViewRow row in GVMec.Rows)
        //Apply some style settimgs
        GVMec.Caption = "Your caption";
        GVMec.Style.Add("border""1px solid black");
        GVMec.Style.Add("font-family""Arial, Helvetica, sans-serif;");
        StringReader sr = new StringReader(sw.ToString());
        //creating new pdf document
        Document pdfDoc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);




No comments:

Post a Comment