Click here to Skip to main content
13,348,874 members (77,362 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 5 Oct 2005

Create a Web Based Non-Typed Crystal Reports Application

, 5 Oct 2005
Rate this:
Please Sign up or sign in to vote.
This article explains how to build a generic and data bound Web based Crystal Reports server.


The .NET Crystal Reports engine supports two kinds of reports: strong typed and non–typed. The strong typed reports bind reports into the executable binary at design time via dataset classes, and the report files are not required at runtime. Non-typed reports bind reports at runtime, and dataset classes are not included into the application at design time, but report files are physically required to be present with binary assembles at runtime.

While each type of report has its own pros and cons, the non-typed reports have two advantages. First, it provides more scalability and flexibility. Since applications do not compile a dataset class into the executable, when a new report is added, the only thing that needs to be done is to create a new report file, and put it into the report server. Meanwhile, since report design and creation are separated from the report applications, the executable assembly can remain “static” and generic to report files. The cost and effort to maintain, deploy and update applications can be significantly reduced.

Using the code

Create a simple report file

  1. Create a dumb Windows application project. (Visual Studio .NET does not allow to create a Crystal Reports file without creating a project first).
  2. Add new item, and select Crystal Reports from the dialog, name the report file as Report1.rpt, and then click Open.
  3. On the Crystal Reports Gallery dialog, select Blank Report, then click OK.
  4. Right click the mouse on the report working area, and select Database, Add/Remove Database… from the context menu. Then select OLEDB (ADO), and OLEDB Provider for SQL Server, from the OLEDB (ADO) dialog, and type in the required information.
  5. On the Database Expert dialog, select the table Titles from the Pubs/Database/Tables tree.
  6. Select fields on the Report design screen, then save the report file.

Create a Web based Reporting application

The next step is to create an ASP.NET application to process and present this report:

  1. Create a new ASP Web application, name the project as MyReport. Rename the default webform1.aspx to main.aspx (you can keep default names if you like).
  2. Add CrystalDecisions to project reference from the Solution view.
  3. Place the code in the source main.aspx Page_Load section into your Page_Load event section, and then build the solution.

All codes are wrapped in the Page_Load event. It does the following:

  1. Creates a Crystal Reports object instance, parses the report name from the QueryString, and loads the report file into the Report object.
  2. Applies logon information to the Report object.
  3. Passes additional parameters (if any) from the QueryString, passes them into the Report object.
  4. Export the report into PDF format (or any other format), and write back to the client browser.

The code for loading the report is as follows:

String reportFile=Request.QueryString.Get("Report") +".rpt";
String appPath=MapPath(".");
String reportPath = appPath + "\\" + reportFile;
ReportDocument theReport=new ReportDocument();
theReport.Load (reportPath);

After the report is loaded, we can apply the logon information to the Report object:

CrystalDecisions.Shared.ConnectionInfo conn=new ConnectionInfo();
CrystalDecisions.CrystalReports.Engine.Table myTable; 
CrystalDecisions.Shared.TableLogOnInfo myLog;
conn.UserID ="sa";
    myLog = myTable.LogOnInfo;
    myLog.ConnectionInfo= conn;
    myTable.Location= myLog.TableName;

Exporting the report file to PDF (or another format), and writing back to the client browser are quite simple:

System.IO.MemoryStream m;
m= (System.IO.MemoryStream)theReport.ExportToStream(
Response.End ();

Now put the report1.rpt file in the same directory as the ASP page. We can test the Web application using a URL like this:


Report with Parameters

To illustrate the report with parameters, we create a stored procedure "Get_Title1" with the following code:

Create procedure Get_Title1(@price float ,@pubdate datetime)
set nocount on
select Title, type, price, notes, pubdate from titles
where price<=@price and pubdate<=@pubdate

Then we create a new report file, report2.rpt. It should be identical to report1.rpt, except it will use the stored procedure "Get_title1" as table name. At the report design screen, when "Get_title1" is selected, parameters @price and @pubdate also show up.

At the web Page_Load event, we need code to process the parameters, as follows:

String paramName,paramValue;
CrystalDecisions.Shared.ParameterValues pList = new  ParameterValues()
CrystalDecisions.Shared.ParameterDiscreteValue pV=new ParameterDiscreteValue ();
for( int i=0;i<theReport.DataDefinition.ParameterFields.Count;i++)
    pV.Value =paramValue;

To run the report with parameters, the URL to be used looks like this:


Dynamic SQL Statement as Parameter

Predefined parameters cannot always meet the needs of a complicated query. Although at design time, Crystal Reports can create a report derived from a SQL command, it disallows to access and modify a SQL command at runtime. To workaround this, we can use the feature of SQL Server executing dynamical SQL statements to generate a report, then pass a SQL string to run the report from the URL.

First, let's create a stored procedure to execute a dynamic SQL statement:

Create procedure Get_Title2(@sqlstring varchar(2000))
set nocount on
create table #temp
(title_id char(6),title varchar(80),type char(12),
pub_id char(4),price money,advance money,
royalty int,ytd_sale int, notes varchar(200), pubdate datetime)
insert into #temp
select * from #temp
drop table #temp

Then create report3.rpt derived from the stored procedure 'Get_title2'. Save it and run the URL like:

http://localhost/myreport/main.aspx?report=report3&@sqlstring=select * from 
titles where type='business' and price<20 and upper(title) like '%COMPUTER%'


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
like to write simple codes to solve complicated problems.

You may also be interested in...


Comments and Discussions

GeneralNeed to open the pdf in open model window Pin
karthiBalu20-Mar-07 5:52
memberkarthiBalu20-Mar-07 5:52 

Your code is very useful.If i try to use response.redirect or server.transfer.I can see the report in pdf format but if use open model window( the report is not rendering in the pdf?what i have to do?please help me
GeneralCrystal Report Help Pleaseeee Pin
ashokjangid13-Nov-06 0:07
memberashokjangid13-Nov-06 0:07 
GeneralCrystal Report Help Pin
ashokjangid13-Nov-06 0:07
memberashokjangid13-Nov-06 0:07 
GeneralCrystal Report graphs Pin
Jesu.Official6-Nov-06 1:58
memberJesu.Official6-Nov-06 1:58 
GeneralServererror on Application... Pin
Jan Waiz9-Jan-06 0:38
memberJan Waiz9-Jan-06 0:38 
GeneralRe: Servererror on Application... Pin
junyaoxu9-Jan-06 4:00
memberjunyaoxu9-Jan-06 4:00 
GeneralRe: Servererror on Application... Pin
terror2310-Jan-07 20:52
memberterror2310-Jan-07 20:52 
GeneralLogon Pin
Aubyone23-Nov-05 19:58
memberAubyone23-Nov-05 19:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 5 Oct 2005
Article Copyright 2005 by junyaoxu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid