Using SAP Crystal Report in ASP.NET

Updated on     Kisan Patel

This tutorial explains how to create a basic crystal report and integrate it to in ASP.NET website using CrystalReportViewer control and also how to export Crystal Report as PDF.

Download Source Code

Crystal Reports is a standard, powerful, and dynamic tool used to create attractive and interactive report, quickly and easily.

Lets start by creating ASP.NET Empty Web Application and name it to “SAPDemo”. Now, add the Default.aspx Web Form into the project.

The following figure shows the table structure and the data in table.

crystal_report_table

There are so many ways to pass the data to Crystal Report. Here I am using DataSet (.xsd file) to bind Crystal Report.

To add the DataSet, Right click to Solution => Add => New Item => Data => Select DataSet and name it to ds_student.xsd and click OK.

crystal_report_dataset_1

Now you can see the ds_student.xsd dataset file opened in Visual Studio. Here you need to create one Data Table with all the columns names you need to shown in Crystal Report. To Create Data Table, see the below screenshot and rename it to dt_student.

crystal_report_datatable_1

Now you can see Data Table created in the Dataset. Next, we need to add the columns names need to shown in Crystal Report. Please note that this Data Table column names and data types should be the same as your table in database.

crystal_report_datatable_2

Now, Lets add Crystal Report to Default.aspx page. To do that, Go to Solution Explorer => Right click on the Project => Add New Item => Reporting => Select “Crystal Report” and name it to StudentReport.rpt then click Add button.

crystal_report_screen1

The Crystal Reports Gallery dialog box appears, Select “Using the Report Wizard” radio button and click OK button, as shown in below screenshot:

crystal_report_screen2

Now expand the ADO.NET DataSets folder. There you can see the DataSet (ds_student) we created. Under ds_student, you can see the Data Table we created. So select this Data Table and click the arrow button to move to the Selected Tables list box as shown in the below:

crystal_report_screen3

Just click the Finish button. Now you can see the Crystal Report opened in Visual Studio. In your left side, you can see Field Explorer. In case you cannot see it there, just go to menu Crystal Reports => Field Explorer. Field Explorer View is shown in below:

crystal_report_screen4

Open the Field Explorer window and drag the Columns such as name, roll_no and course of the table in the Section3 (Details), as shown in below screenshot:

crystal_report_screen5

Now, open Default.aspx page and add the CrystalReportViewer controls as shown in below code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SAPDemo.Default" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title>Crystal Report Demo</title>
</head>
<body>
   <form id="form1" runat="server">
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
 DisplayGroupTree="False"/>
   </form>
</body>
</html>

Then write the below code in Default.aspx.cs page to bind data to Crystal report.

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentCon"].ConnectionString);
              con.Open();

              ds_student ds = new ds_student();
              SqlCommand com = new SqlCommand("SELECT * FROM Student", con);
              SqlDataAdapter sdt = new SqlDataAdapter(com);
              sdt.Fill(ds.dt_student);
              con.Close();

              ReportClass rptDoc = new ReportClass();

              rptDoc.FileName = Server.MapPath("~/StudentReport.rpt");

              rptDoc.Load();
              rptDoc.SetDataSource(ds);

              CrystalReportViewer1.ReportSource = rptDoc;
     }
}

If you want to export Crytal Report to as PDF file then use below code:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentCon"].ConnectionString);
            con.Open();

            ds_student ds = new ds_student();
            SqlCommand com = new SqlCommand("SELECT * FROM Student", con);
            SqlDataAdapter sdt = new SqlDataAdapter(com);
            sdt.Fill(ds.dt_student);
            con.Close();

            ReportClass rptDoc = new ReportClass();

            rptDoc.FileName = Server.MapPath("~/StudentReport.rpt");

            rptDoc.Load();
            rptDoc.SetDataSource(ds);

            Stream stream = rptDoc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); // Assuming you have a method that does this.
            //BinaryReader reader = new BinaryReader(stream);

            HttpResponse response = HttpContext.Current.Response;
            response.ClearContent();
            response.ContentType = "application/pdf";
            response.AddHeader("Content-Disposition", "attachment; filename=file.pdf");
            CopyStream(stream, response.OutputStream);

            // End the response to prevent further work by the page processor.
            response.End();
        }


    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
}

Here is the screenshot of the exported PDF document:

crystal_report_pdf


ASP.NET SAP Crystal Report

Leave a Reply