Building Reports using ASP.NET and Crystal Reports - Part 5 - Creating Reports as a Web Service
page 5 of 6
by Vince Varallo
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 31884/ 66

Step 4: Create the Web Site

1.    Launch another instance of Visual Studio.

2.    Select File -> New Web Site from the menu.

3.    Choose ASP.NET Web Site from the list of templates.

4.    Choose "File" for the location.

5.    Enter "ReportingWebSite" for the name of the web site.  The path should be "..\Visual Studio 2008\WebSites\ReportingWebSite"

6.    Click the OK button.

7.    Open the Markup for the Default.aspx page if it isn't already open.

8.    Copy the following Register directive after the Page directive.

Listing 1

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

9.    Enter the following between the div tags.

Listing 2

Select a customer:<asp:DropDownList ID="ddlCustomer" runat="server">
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click" 
    Text="Preview" />
<br />
<br />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
    AutoDataBind="true" />

10. Click the Design button.  Your design environment should look like the following image.

11. Double click on the Preview button to generate the click event handler.

12. Enter the following code for the click event.

Listing 3

protected void btnPreview_Click(object sender, EventArgs e)
    CrystalReportViewer1.Visible = true;

13. Create the BindReport method.

Listing 4

private void BindReport()
    CrystalReportViewer1.ReportSource = 
    CrystalReportViewer1.SelectionFormula = "{SalesOrderHeader.ContactID} = " 
        + ddlCustomer.SelectedItem.Value;

14. ReplaceCrystalReportViewer1.ReportSource = "http://localhost:2632/ReportingService/InvoiceService.asmx"; with the path to the web service you created in step 3.  If you have the browser still running from step 3 you can copy and paste the URL from the browser.

15. Add the code to the Form Load event to load the customer drop down list.

Listing 5

protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
    //Load the drop down list with customers.
    SqlConnection cn = new SqlConnection(
    SqlCommand cmd = new SqlCommand(
  "SELECT DISTINCT LastName + ', ' + FirstName AS Name, Person.Contact.ContactID " +
        "FROM Sales.SalesOrderHeader " +
        "INNER JOIN Person.Contact " +
        "ON Sales.SalesOrderHeader.ContactID = Person.Contact.ContactID " +
        "ORDER BY LastName + ', ' + FirstName", cn);
    SqlDataReader dr = cmd.ExecuteReader();
    ddlCustomer.DataSource = dr;
    ddlCustomer.DataTextField = "Name";
    ddlCustomer.DataValueField = "ContactId";
    CrystalReportViewer1.Visible = false;
    if (CrystalReportViewer1.Visible == true)
      //Rebind the report.

16. Add the connection string for the AdventureWorks database to the web.config file.  Be sure to set the server, user name and password for your database.

Listing 6

  <add name="AdventureWorks" connectionString=
    "Data Source=YOURSERVER\SQL2008;User ID=aspalliance;Password=aspalliance;
Initial Catalog=AdventureWorks;"/>

17. Add the following using statements to the top of you file.

Listing 7

//Custom using statements
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

18. Press F5 to run the web site.  You'll be prompted to enable debugging for the web site.  Click the OK button.

19. The Default page should be displayed and the customer drop down list should be populated with the list of customers.

20. Select Abercrombie, Kim from the drop down list and click the Preview button.

21. You should see the following report.

The BindReport method does all the work.  The first line tells the viewer where the report is located.

Listing 8

CrystalReportViewer1.ReportSource = 

The second line sets the selection formula for the report so only the selected customer is displayed in the report.

Listing 9

CrystalReportViewer1.SelectionFormula = "{SalesOrderHeader.ContactID} = " 
    + ddlCustomer.SelectedItem.Value;

View Entire Article

User Comments

Title: Possibility in crystal report 2010   
Name: thantowi jauhari
Date: 2011-09-14 11:42:35 AM
is it work for crystal report 2010 ??
Title: Crystal Reports Hosting   
Name: crystaly
Date: 2010-11-24 1:43:36 AM
Just found your nice article. thx
Title: Crystal Rpts Web Service   
Name: Doug
Date: 2010-10-14 4:57:56 AM
Hello Vince,
I was wondering if you had issues with load times while downloading the web service everytime you open the browser again. It takes 15 seconds to run my report the first time then 2 seconds everytime thereafter. Thoughs?
Title: ASP.NET Crystal Reports Hosting   
Name: rajanish
Date: 2009-10-23 1:27:06 AM
good article thanks
if i have more than one reports(.rpt) in web service then how we can call dynamically from report web site
Title: ASP.NET Crystal Reports Hosting   
Name: Helen
Date: 2009-10-14 1:23:14 PM
Excellent Articles!

Nice crystal reports guidance!

Title: Mr.   
Name: Rinoy
Date: 2009-10-13 8:31:30 AM
good article thanks
if i have more than one reports(.rpt) in web service then how we can call dynamically from report web site

Title: Mr   
Name: Kirt Darji
Date: 2009-10-07 12:55:13 AM
Good Artical

crystle Report not run on live server for that what we have to do.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-02-25 4:08:27 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search