Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Developing Web Report using Crystal Report in .NET 2005.

0.00/5 (No votes)
29 Mar 2006 1  
The report is build on XML Schema and the data and binded the report at runtime.

Sample Image - ReportView.jpg

Introduction

In this article I have tried to cover a cycle of developing a web report using Crystal Report in .NET 2005. I haven't put any effort for the butification of the report. The report is just showing the Employee master data from the Northwind database of SQL Server.

The report is build on XML Schema and the data and binded the report at runtime.


Requirements

  1. Microsoft C#.NET 2005
  2. Windows 2000/2003/XP
  3. SQL Server 2000/2005(Optional). I have used SQL Server to fecth data. If in case SQL Server is not available then change the connection string in web.config and connection related syntax in Default.aspx.cs.


Creating your own Report

To start with Reports, you need start .NET 2005 Development Studio.

  1. Start a new ASP .NET Web site, Choose Location as "File System" and provide a path.
  2. Perform "Add New Item", choose "XML Schema".
       Add elements as the picture below.

Sample screenshot

         After saving the elements will look like this, in the XML Editor         


<xs:element name="EmployeeID" type="xs:int" />
<xs:element name="LastName" type="xs:string" />
<xs:element name="FirstName" type="xs:string" />
<xs:element name="Title" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="Address" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="Region" type="xs:string" />
<xs:element name="PostalCode" type="xs:string" />
<xs:element name="Country" type="xs:string" />
<xs:element name="HomePhone" type="xs:string" />

   3.  Perform "Add New Item", choose "Crystal Report".

    1. Give a name for the report.
    2. Choose -- Using the Report Wizard. -- OK.
    3. A Data window will appear, Click on "Create New Connection" and then "ADO .NET".
    4. A connection windows will appear. Provide the XML Schema file that u have created just now. Finish.
    5. The Schema will appear under the "ADO .NET" tag. Choose. And select using > button.
    6. Click Next. Fields windows will appear. Select all using >> button.
    7. Click Finish, for taking shortcut.

    4.  Open the Default.aspx in Design mode. Add CrystalReportViewer control in to it. The source will look like this.

 <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" /> 

    5.  Open the Default.aspx.cs file and paste the following codes.
 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//--SqlClient for SqlConnection and etc.
using System.Data.SqlClient;
//--for CrystalReports's ReportDocument.
using CrystalDecisions.CrystalReports.Engine;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//--Sql string
String strCmd = "";
strCmd += "Select EmployeeID, LastName, FirstName, Title, BirthDate, ";
strCmd += "Address, City, Region, PostalCode, Country, HomePhone ";
strCmd += "From Employees ";
//--Opening Sql Connection
string strConn = ConfigurationManager.AppSettings["connectionstring"];
SqlConnection sqlConn = new SqlConnection(strConn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strCmd, sqlConn);
//--this statement is very important, here the table name should
//--match with the XML Schema table name
da.Fill(ds, "Employees");
//--Closing Sql Connection
sqlConn.Close();
//--(Optional) I have used it to disable the properties
CrystalReportViewer1.DisplayGroupTree = false;
CrystalReportViewer1.HasCrystalLogo = false;
//--Initializing CrystalReport
ReportDocument myReportDocument;
myReportDocument = new ReportDocument();
myReportDocument.Load(Server.MapPath("Employees.rpt"));
myReportDocument.SetDataSource(ds);
//--Binding report with CrystalReportViewer
CrystalReportViewer1.ReportSource = myReportDocument;
CrystalReportViewer1.DataBind();
}
}

    6.  Open the Web.config file and change the connection string under <appSettings> as per your machine (ip address, username, password).


<appSettings>
<add key="connectionString" value="Data Source=9.182.223.80;Initial Catalog=Northwind;Persist Security Info=True;User ID=testUser;Password=password" />

</appSettings>
If in case the file is not available then perform "Add New Item", choose "Web Configuration File", and follow the same thing.

    7.  Using the script

Just to check wheather Employees master table is available with data
   . Open SQL Server Query Analyzer
   . Copy the scripts
   . Paste into Query Analyzer
   . Press F5 to execute

---------------------------------
-----Using Database
---------------------------------
Use Northwind;
---------------------------------
-----Selecting data from Table
---------------------------------
Select EmployeeID, LastName, FirstName, Title, BirthDate,
Address, City, Region, PostalCode, Country, HomePhone
From Employees;
    7.  Now build the Web project. Press F5/Ctrl F5 to view the report. Hope everything will go fine.


 

License

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