Generate pdf document from gridview data and store on web server – Part 59

Video ready, click here to close ×


The HTML and the code used in the demo, can be found at the link below.
http://csharp-video-tutorials.blogspot.com/2013/04/generate-pdf-document-from-gridview.html

Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Please watch Part 58, before proceeding with this video.

Video ready, click here to close ×

Step 1: Create an asp.net web application project. Add a folder with name “PDFDocuments” to the project. We will be using this folder to store the dynamically generated pdf document.

Step 2: Drag and drop a gridview control and a button control on webform1.aspx. Autoformat the gridview control to use “BrownSugar” scheme. Double click on the button control, to generate click event handler method.

Step 3: To generate PDF documents we will be using open source assembly – iTextSharp.dll. This assembly can be downloaded from http://sourceforge.net/projects/itextsharp/. After you download the assembly, add a reference to it, from your web application.
a) In Solution Explorer, right click on the “References” folder and select “Add Reference”
b) Browse to the folder where you have downloaded the assembly and Click OK.

Step 4: Add the following “USING” statements, in your code-behind file.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

Step 5: Copy and paste the following code. The code is well commented and is self-explanatory.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridViewData();
}
}

private void BindGridViewData()
{
string CS = ConfigurationManager.ConnectionStrings[“DBCS”].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter(“Select * from tblEmployee”, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


source

8 comments

  • Hi Sir, Can we convert the pdf into base64 and keep it in a database table. Actually I want to implement this into Dynamics CRM report.

  • hello sir, In my project it is not working. i used item template that bind the multiple column from different tables and show the record easily on page load. but when i click on the export to pdf button it does not show any record related to it although pdf is generated at that time. its tablecell value is null can u tell me the solution for it.

  • Hi sir, can you please tell us how we can print Receipt from Database.
    Eg. A Company's all details are stored in Database and he want to print all customer details at one click on print Button.
    On each page here will be company name, customer name, Sallary for current month.
    How we can achieve that ?

  • Hi venkat, Can you please upload some jQuery and WCF related videos.Thank you very much for your help.

    

  • Hi venkat
    i sent you request for a Some of the Sql Query's Explanation which will be asked in many of the interviews Plz record a video on it which will help very people who are attending interviews and also explain about Debugging Techniques of an application. 

  • Hi Venkat, Many of my friends and myself are waiting for your wcf videos, can you please post them first before MVC. I also sent you an email on this. Can you please let me know any tentative date for wcf videos ?

  • Hi Harika, can we have these 2 topics covered a little while later, as there are so many people waiting for MVC and WCF. Let's get started with these, and some time in the future I will definitely cover garbage collection and the question you have in exporting to excel. I hope you are OK with the plan. Thank you for your patience.

  • hi venkat,
    i have asked how to export from gridview to excel.pls before starting new topic could u show us this .i have u sent u mail….pls check ur mail from harika…in that i have written format of excel…and i want to save excel in client's machine too….so that whenever client wants to see that data….i am requesting to complete this before starting new topic….garbage collector topic there to cover…. is it possible to cover this topic pls…

Leave a Reply

Your email address will not be published. Required fields are marked *