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.
Image 1.
Open Visual Studio and select File -> New Web Site.
Image 2.
Right-click on the project in Solution Explorer then select Add New Item then select DataSet.
Image 3.
Now Right-click and seelct Add -> 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.
Image 5.
Now right-click on the project in Solution Explorer then select Add New Item then select Crystal Report.
Image 6.
Image 7.
Select Project Data -> ADO.NET Data Sets then select your new Data Set then select your table then click Next.
Image 8.
Now select the columns for the report and click on Finish.
Image 9.
Now on your aspx page add a text box, button and a report viewer as in the following:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeReport.aspx.cs" Inherits="EmployeeReport" %>
- <%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
- Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Employee Report</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table cellpadding="10" cellspacing="10" width="70%" height="300px" align="center"
- style="border: solid 2px gray;">
- <tr>
- <td align="center" style="background-color: SkyBlue;">
- Employee Name:#
- <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
- <asp:Button ID="btnShowReport" runat="server" Text="Show Report" OnClick="btnShowReport_Click" />
- </td>
- </tr>
- <tr>
- <td align="center">
- <asp:Panel ID="pnlReport" runat="server" Height="400px">
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
- </asp:Panel>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Now the aspx.cs code is:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data;
- using System.Data.SqlClient;
- using CrystalDecisions.CrystalReports.Engine;
-
-
- public partial class EmployeeReport : System.Web.UI.Page
- {
- SqlConnection con;
- SqlDataAdapter da = new SqlDataAdapter();
- DataSet ds = new DataSet();
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void btnShowReport_Click(object sender, EventArgs e)
- {
- con = new SqlConnection(@"Data Source=MyPC\SqlServer2k8;Integrated Security=True;Initial Catalog=Test");
- da = new SqlDataAdapter("select * from Employee where EmployeeName like '%" + txtEmployeeName.Text + "%' ", con);
- da.Fill(ds);
- ReportDocument rd = new ReportDocument();
- rd.Load(Server.MapPath("ShowEmployeeReport.rpt"));
- rd.SetDataSource(ds.Tables[0]);
- CrystalReportViewer1.ReportSource = rd;
- }
- }
Now run the application:
Image 10.
Image 11.