Search & Display Data in Gridview Then Export GridView as PDF Using ASP.Net

Introduction

This article shows how to search the data from a database and display it in a GridView and how to export a GridView into a PDF using ASP.Net.
Database structure.
  1. CREATE TABLE [dbo].[Employe]  
  2.  (  
  3.     [Empid] [intNOT NULL,  
  4.     [Empname] [varchar](50) NOT NULL,  
  5.     [Empcity] [varchar](50) NOT NULL,  
  6.     [Empaddress] [varchar](50) NOT NULL,  
  7.     [Emailid] [varchar](50) NULL,  
  8. )  
 
 
In the preceding table. we have five columns, they are Empid(primary key),Empname,Empcity,Empaddress,Emailid. Then inserting the value in the table.
  1. insert into Employe values(5,"tajbar","noida","uttarakhand","[email protected]",)  
  2. insert into Employe values(8,"atul","noida","uttarakhand","[email protected]",)  
  3. insert into Employe values(10,"taj","roorkee","uttarakhand","[email protected]",)  
  4. insert into Employe values(10,"tajbar","haridwar","up","[email protected]",)  
Now Create application to searching the record from database.

Open Microsoft Visual Studio 2013 then in the Start Page select New Project => Web => VS 2012 then select ASP.Net Empty Web Application and enter the project name as you wish and click the OK button.

Right-click on the project name then select Add New Item and select Web Form.

HTML Code
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.     <div>  
  8.     <table>  
  9.     <tr>  
  10.     <td>   
  11.        Search  
  12.         </td>  
  13.         <td>  
  14.         <asp:TextBox ID="textinput" runat="server"></asp:TextBox>  
  15.         </td>  
  16.         <td>   
  17.         <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />  
  18.         </td>  
  19.           
  20.         </tr>  
  21.    
  22. </table>  
  23. <table><tr><td><p><asp:Label ID="Label2" runat="server"></asp:Label>  </p></td></tr>  
  24.   
  25. </table>  
  26.   
  27.     </div>  
  28.         <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  29.         <p>  
  30.              </p>  
  31.         <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Export in PDF" />  
  32.     </form>  
  33. </body>  
  34. </html>  
The following is the output of the preceding code.

gridform.aspx.cs code
  1. sing System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Configuration;  
  9. using iTextSharp.text;  
  10. using iTextSharp.text.pdf;  
  11. using iTextSharp.text.html.simpleparser;  
  12. using System.IO;  
  13.   
  14. namespace showdataingridview  
  15. {  
  16.     public partial class gridform : System.Web.UI.Page  
  17.     {  
  18.         string cons = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;  
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.               
  22.   
  23.         }  
  24.   
  25.         public  DataSet Bind()  
  26.         {  
  27.   
  28.             SqlConnection con = new SqlConnection(cons);  
  29.             SqlCommand cmd = new SqlCommand("select * from Employe where Empname like'" + textinput.Text + "%'",con);  
  30.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  31.             DataSet ds = new DataSet();  
  32.             da.Fill(ds);  
  33.             if (!object.Equals(ds, null))  
  34.             {  
  35.                 if (ds.Tables[0].Rows.Count>0)  
  36.                 {  
  37.                     GridView1.DataSource = ds.Tables[0];  
  38.                     GridView1.DataBind();  
  39.                 }  
  40.   
  41.             }  
  42.   
  43.               
  44.             return ds;  
  45.         }  
  46.   
  47.         protected void Button1_Click(object sender, EventArgs e)  
  48.   
  49.         {  
  50.             
  51.              DataSet ds=  Bind();  
  52.   
  53.              if (!object.Equals(ds, null))  
  54.              {  
  55.                  if (ds.Tables[0].Rows.Count>0)  
  56.                  {  
  57.                      GridView1.Visible = true;  
  58.                      textinput.Text = " ";  
  59.                      Label2.Text = " ";  
  60.                  }  
  61.   
  62.   
  63.                  else  
  64.                  {  
  65.                      GridView1.Visible = false;  
  66.                      Label2.Visible = true;  
  67.                      Label2.Text = "not available in the record";  
  68.   
  69.                  }  
  70.              }  
  71.         }  
  72. }  
Now run the application, it will look as in the following:
 
Now enter some characters into the TextBox that do not match the specified table's records. It will show the message as in the following:
 
 
If you do not enter any value into the TextBox then it shows the message as in the following:
 
  
 
Now enter the specific name and click on the "Go" button to search the records specific to the name as follows: 
 
  
In the preceding figure, display the data in the GridView. Now we will export the GridView data into a PDF.
 
Let's see the procedure to export the GridView data into a PDF.
 
We need to install iTextSharp. Run the following command in the Package Manager Console
 
pm> Install-Package iTextSharp
 
Code
 
In the code behind add the following namespaces:
  1. using iTextSharp.text;  
  2. using iTextSharp.text.pdf;  
  3. using iTextSharp.text.html.simpleparser;  
Double-click on the button "Export in PDF" and write the following code there:
  1. protected void Button2_Click(object sender, EventArgs e)    
  2. {    
  3.     GetPdf();    
  4.         
  5.         
  6. }    
  7.   
  8. public void GetPdf()    
  9. {    
  10.   
  11.     Response.ContentType = "application/DOC";    
  12.     Response.AddHeader("content-disposition""attachment;filename=AtulRawat.pdf");    
  13.     Response.Cache.SetCacheability(HttpCacheability.NoCache);    
  14.     StringWriter s_w = new StringWriter();    
  15.     HtmlTextWriter h_w = new HtmlTextWriter(s_w);    
  16.   
  17.     GridView1.RenderControl(h_w);    
  18.     StringReader sr = new StringReader(s_w.ToString());    
  19.     Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);    
  20.     HTMLWorker htmlparser = new HTMLWorker(pdfDoc);    
  21.     PdfWriter.GetInstance(pdfDoc, Response.OutputStream);    
  22.     pdfDoc.Open();    
  23.     htmlparser.Parse(sr);    
  24.     pdfDoc.Close();    
  25.     Response.Write(pdfDoc);    
  26.     Response.End();    
  27.     GridView1.AllowPaging = true;    
  28.     GridView1.DataBind();    
  29.   
  30.     GridView1.HeaderRow.Style.Add("width""15%");    
  31.     GridView1.HeaderRow.Style.Add("font-size""10px");    
  32.     GridView1.Style.Add("text-decoration""none");    
  33.     GridView1.Style.Add("font-family""Arial, Helvetica, sans-serif;");    
  34.     GridView1.Style.Add("font-size""8px");    
  35.   
  36.     
  37. }    
  38. public override void VerifyRenderingInServerForm(Control control)    
  39. {    
  40.     /* Verifies that the control is rendered */    
  41. }   
 
If you click on the Export to PDF button, the PDF file is generated. The following table is created inside the PDF file.
 
 
Summary
 
In this article we learned how to search the data from a database and display it in a GridView and learned how to export a GridView into a PDF using ASP.Net.

Up Next
    Ebook Download
    View all
    Learn
    View all