Crystal Report With Search Functionality in ASP.NET

Here I will display an Employee report. The user can see any employee report by typing his/her name.

Now we will learn this step-by-step.

The following is my SQL Server table, for which I will show a Crystal Report.

crystal report
                                                                           Image 1.

Open Visual Studio and select File -> New Web Site.

New Web Site
                                                                        Image 2.

Right-click on the project in Solution Explorer then select Add New Item then select DataSet.

Select DataSet
                                                                     Image 3.

Now Right-click and seelct Add -> Data Table.

Data Table
                                                                     Image 4.

Now right-click on the Header and select Add -> Column.

Be sure the column name and their Data type are the same as in your data table. You can change the data type by selecting your added column. Then right-click on something and select the Data type property.

Data type property
                                                                     Image 5.

Now right-click on the project in Solution Explorer then select Add New Item then select Crystal Report.

Select Crystal Report
                                                                        Image 6.

standerd
                                                Image 7.

Select Project Data -> ADO.NET Data Sets then select your new Data Set then select your table then click Next.

Click Next
                                                                     Image 8.

Now select the columns for the report and click on Finish.

click on finish
                                                                        Image 9.

Now on your aspx page add a text box, button and a report viewer as in the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeReport.aspx.cs" Inherits="EmployeeReport" %>  
  2. <%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"  
  3.     Namespace="CrystalDecisions.Web" TagPrefix="CR" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Employee Report</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <table cellpadding="10" cellspacing="10" width="70%" height="300px" align="center"  
  13.             style="border: solid 2px gray;">  
  14.             <tr>  
  15.                 <td align="center" style="background-color: SkyBlue;">  
  16.                     Employee Name:#  
  17.                     <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>  
  18.                     <asp:Button ID="btnShowReport" runat="server" Text="Show Report" OnClick="btnShowReport_Click" />  
  19.                 </td>  
  20.             </tr>  
  21.             <tr>  
  22.                 <td align="center">  
  23.                     <asp:Panel ID="pnlReport" runat="server" Height="400px">  
  24.                         <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />  
  25.                     </asp:Panel>  
  26.                 </td>  
  27.             </tr>  
  28.         </table>           
  29.     </div>  
  30.     </form>  
  31. </body>  
  32. </html>  
Now the aspx.cs code is:
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data;  
  14. using System.Data.SqlClient;  
  15. using CrystalDecisions.CrystalReports.Engine;  
  16.   
  17.   
  18. public partial class EmployeeReport : System.Web.UI.Page  
  19. {  
  20.     SqlConnection con;      
  21.     SqlDataAdapter da = new SqlDataAdapter();  
  22.     DataSet ds = new DataSet();  
  23.   
  24.     protected void Page_Load(object sender, EventArgs e)  
  25.     {  
  26.   
  27.     }  
  28.     protected void btnShowReport_Click(object sender, EventArgs e)  
  29.     {  
  30.         con = new SqlConnection(@"Data Source=MyPC\SqlServer2k8;Integrated Security=True;Initial Catalog=Test");  
  31.         da = new SqlDataAdapter("select * from Employee where EmployeeName like '%" + txtEmployeeName.Text + "%' ", con);  
  32.         da.Fill(ds);  
  33.         ReportDocument rd = new ReportDocument();  
  34.         rd.Load(Server.MapPath("ShowEmployeeReport.rpt"));  
  35.         rd.SetDataSource(ds.Tables[0]);  
  36.         CrystalReportViewer1.ReportSource = rd;  
  37.     }  
  38. }  
Now run the application:

run the application
                                                                    Image 10.

output
                                                                  Image 11.

 

Up Next
    Ebook Download
    View all
    Learn
    View all