Building Reports using ASP.NET and Crystal Reports - Part 7 Using Subreports to Create Advanced Reports
page 6 of 7
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34381/ 98

Step 6: Viewing the report in an ASP.NET page

Now that you have built the report you can build a web page to display it.  I'll create a simple page that lets the user click a button to preview the report.  We'll do all of this in this Default.aspx page that is part of the web site we created.

1.    Open the Default.aspx page and view its HTML markup.  Add the following Register directive after the Page directive.

<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, 
  PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR"%>

This allows you to use the Crystal Reports Viewer control that comes with Visual Studio.

2.    Add the following code between the div tags.  

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

3.    Open the code behind page and add the following using statements in the code behind.

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

4.    Add the following code to the Page_Load even handler.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Hide the crystal report viewer
        CrystalReportViewer1.Visible = false;         
    }
    else
    {
        //If this is a postback the rebind the report so the paging works.
        if (CrystalReportViewer1.Visible == true)
        {
            //Rebind the report.
            BindReport();
        }
    }
}

This will hide the Crystal Report Viewer Control when the user initially navigates to the page.  The postback check code is needed because if the user is paging through the report a postback event is fired and the report needs to be rebound to the Viewer control.

5.    Add the following code for the Preview button's click event.

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

6.    Now add the following custom methods.

public void BindReport()
{
  ReportDocument report = new ReportDocument();
  report.Load(Server.MapPath("SubReport.rpt"));
 
  SetTableLocation(report.Database.Tables, "YourServer""AdventureWorksLT");
 
  SetTableLocation(report.Subreports[0].Database.Tables, "YourServer", 
                   "AdventureWorks");
 
  CrystalReportViewer1.ReportSource = report;                
}
 
private void SetTableLocation(Tables tables, string server, string database)
{
  ConnectionInfo connectionInfo = new ConnectionInfo();
 
  connectionInfo.ServerName = server;
  connectionInfo.DatabaseName = database;
  connectionInfo.UserID = "aspalliance";
  connectionInfo.Password = "aspalliance";
 
  foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
  {
      TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
      tableLogOnInfo.ConnectionInfo = connectionInfo;
      table.ApplyLogOnInfo(tableLogOnInfo);
  }
}

The first method creates an instance of the ReportDocument class.  This represents the report that you created earlier and allows you to manipulate it at runtime.  The SetTableLocation() method sets the table location for each table in the report.  Again, this assumes you've created an "aspalliance" SQL Login and have given it access to the database.  The ReportDocument class exposes the sub report as a collection.  You can have more than one subreport in a report but for this example we only have one so we have to set the location for the subreport with the index of 0.  The Crystal Report Viewer's source is then set to the report object. 

You can now run the project by pressing F5. Click the Preview button.


View Entire Article

User Comments

Title: Crystal Report   
Name: B.Shah
Date: 2010-09-21 6:43:53 AM
Comment:
Nice Article..Thanks
Title: Donor Database Administrator   
Name: Abdillahi Jibril
Date: 2010-01-27 11:00:37 AM
Comment:
I have medium to advanced level experience in Crystal Report, and your article helps me improve my skills to be better Crystal Report Developer.
Title: crystal reports   
Name: joohitha
Date: 2010-01-05 11:47:19 PM
Comment:
nice article

Tks vincey
Title: crystal reports tutor   
Name: lokesh
Date: 2009-12-28 5:02:03 AM
Comment:
Thanks vincey
very nice and very help full for crystal report beginners.
i will be at lokesh_bsc@yahoo.com

Product Spotlight
Product Spotlight 



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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 11:32:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search