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.
In the preceding table. we have five columns, they are Empid(primary key),Empname,Empcity,Empaddress,Emailid. Then inserting the value in the table.
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 xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- Search
- </td>
- <td>
- <asp:TextBox ID="textinput" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
- </td>
-
- </tr>
-
- </table>
- <table><tr><td><p><asp:Label ID="Label2" runat="server"></asp:Label> </p></td></tr>
-
- </table>
-
- </div>
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- <p>
- </p>
- <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Export in PDF" />
- </form>
- </body>
- </html>
The following is the output of the preceding code.
gridform.aspx.cs code
- sing System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
- using System.IO;
-
- namespace showdataingridview
- {
- public partial class gridform : System.Web.UI.Page
- {
- string cons = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
- }
-
- public DataSet Bind()
- {
-
- SqlConnection con = new SqlConnection(cons);
- SqlCommand cmd = new SqlCommand("select * from Employe where Empname like'" + textinput.Text + "%'",con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- if (!object.Equals(ds, null))
- {
- if (ds.Tables[0].Rows.Count>0)
- {
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- }
-
- }
-
-
- return ds;
- }
-
- protected void Button1_Click(object sender, EventArgs e)
-
- {
-
- DataSet ds= Bind();
-
- if (!object.Equals(ds, null))
- {
- if (ds.Tables[0].Rows.Count>0)
- {
- GridView1.Visible = true;
- textinput.Text = " ";
- Label2.Text = " ";
- }
-
-
- else
- {
- GridView1.Visible = false;
- Label2.Visible = true;
- Label2.Text = "not available in the record";
-
- }
- }
- }
- }
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:
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
Double-click on the button "Export in PDF" and write the following code there:
- protected void Button2_Click(object sender, EventArgs e)
- {
- GetPdf();
-
-
- }
-
- public void GetPdf()
- {
-
- Response.ContentType = "application/DOC";
- Response.AddHeader("content-disposition", "attachment;filename=AtulRawat.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- StringWriter s_w = new StringWriter();
- HtmlTextWriter h_w = new HtmlTextWriter(s_w);
-
- GridView1.RenderControl(h_w);
- StringReader sr = new StringReader(s_w.ToString());
- Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
- HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
- PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
- pdfDoc.Open();
- htmlparser.Parse(sr);
- pdfDoc.Close();
- Response.Write(pdfDoc);
- Response.End();
- GridView1.AllowPaging = true;
- GridView1.DataBind();
-
- GridView1.HeaderRow.Style.Add("width", "15%");
- GridView1.HeaderRow.Style.Add("font-size", "10px");
- GridView1.Style.Add("text-decoration", "none");
- GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
- GridView1.Style.Add("font-size", "8px");
-
-
- }
- public override void VerifyRenderingInServerForm(Control control)
- {
-
- }
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.