Here is this Crystal Reports report. I will be showing data from more than one table in ASP.NET.

The following 3 tables I am using to create a Crystal Report.

1. Customer

Customer
Image 1.

2. Product


Product
Image 2.

3. Cust_Prod_Order

Cust Prod Order
Image 3.

Now open Visual Studio and go to File -> New Web Site.

New Web Site
Image 4.

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

Crystal Report
Image 5.

Add
Image 6.

Here expand Create New Connection then select OLE DB (ADO) then a pop-up window will open then select Microsoft OLE DB Provider for SQL Server then click Next.

Microsoft OLE DB Provider
Image 7.

Now enter your SQL Server details.

SQL server Details
Image 8.

SQL server
Image 9.

Now select your database then select all your tables and move them over to Selected Tables.

Select Your All Tables
Image 10.

Now you can see your tables with relationships.

relationship
Image 11.

Now select the columns to show in the reports.

reports
Image 12.

Now you can see your report is ready. All the columns are already in the Details sections. You can remove any column or you can add a new column by dragging and dropping from the Field Explorer to the report. Here I did some formatting like header text background, detail column colour and so on.

column colour
Image 13.

Now in the Field Explorer select Formula Fields -> Right-click - New.

Select Formula Fields
Image 14.

Provide a name then click Use Editor.

Click Use Editor
Image 15.

Now select your Column Name and drag it to the following editor window and write Sum formula.

Select Your Column Name
Image 16.

Now click on Save and Close.

Now drag the formula fields to the report footer.

Report Footer
Image 17.

Now to add a Report Viewer to show this Crystal Report. In the Default.aspx page drag and drop a CrystalReportViewer from the toolbox as in the following.

CrystalReportViewer
Image 18.

My aspx code is:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  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>Crystal Report – Sum of a Column</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <table cellpadding="10" cellspacing="10" width="70%" height="300px" align="center"
  12. style="border: solid 2px gray;">
  13. <tr>
  14. <td align="center" style="background-color: SkyBlue;">
  15. <span style="font-family: Times New Roman; font-size: 18pt; color: Green;">Customer
  16. Product Order Detail Report</span>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td align="center">
  21. <asp:Panel ID="pnlReport" runat="server" Height="400px">
  22. <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
  23. </asp:Panel>
  24. </td>
  25. </tr>
  26. </table>
  27. </form>
  28. </body>
  29. </html>

Now on Page_Load event write the following code:

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using CrystalDecisions.CrystalReports.Engine;
  13. public partial class _Default : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. ReportDocument cryRpt = new ReportDocument();
  18. cryRpt.Load(Server.MapPath("EmployeeCrystalReport.rpt"));
  19. CrystalReportViewer1.ReportSource = cryRpt;
  20. }
  21. }

Now run your application:

Run your Application
Image 19.