Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

Developing Web Report using Crystal Report in .NET 2005.

Rate me:
Please Sign up or sign in to vote.
2.90/5 (14 votes)
29 Mar 20062 min read 183.1K   2.3K   52   28
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         

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

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

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

<BR><appSettings><BR>     <add key="connectionString" value="Data Source=9.182.223.80;Initial Catalog=Northwind;Persist Security Info=True;User ID=testUser;Password=password" /><BR>     <BR> </appSettings> <BR>   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

---------------------------------<BR>-----Using Database<BR>---------------------------------<BR>Use Northwind; <BR>---------------------------------<BR>-----Selecting data from Table<BR>---------------------------------<BR>Select EmployeeID, LastName, FirstName, Title, BirthDate, <BR> Address, City, Region, PostalCode, Country, HomePhone<BR> 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


Written By
Web Developer
India India
Suranjan Nandi has 7 years of working experience in Software Development using Microsoft Technology. During this period he was involved with .NET Technologies, COM, Client Server Architecture, Multi-tier Architecture, Crystal Reports, Database Analysis & Designing in Microsoft Environment.

Comments and Discussions

 
QuestionSystem.Runtime.InteropServices.COMException: Not enough memory for operation. Pin
Member 1105673610-Aug-16 3:04
Member 1105673610-Aug-16 3:04 
QuestionCrystalReport 8.5 Based on Queryfile in .net 2.0 Pin
anand45612-Dec-11 0:14
anand45612-Dec-11 0:14 
GeneralMy vote of 1 Pin
db conner13-Apr-10 16:59
db conner13-Apr-10 16:59 
GeneralMy vote of 1 Pin
shankar.koppella24-Feb-10 22:40
shankar.koppella24-Feb-10 22:40 
GeneralMy vote of 1 Pin
DungVinh11-Dec-09 19:23
DungVinh11-Dec-09 19:23 
GeneralGreat Article Pin
briogene19-Feb-09 2:26
briogene19-Feb-09 2:26 
QuestionAdditional window appearing while clicking on print button of crystal report Pin
Manoj Tillu25-Aug-08 23:12
Manoj Tillu25-Aug-08 23:12 
GeneralGreat article Pin
sergey.rush11-May-08 3:26
sergey.rush11-May-08 3:26 
Generalxml schema Pin
senorita312825-Apr-08 4:56
senorita312825-Apr-08 4:56 
QuestionTrying to show the correct output using two tables Pin
charm_depths_274-Sep-07 20:51
charm_depths_274-Sep-07 20:51 
GeneralUnable To Display MySQL DateTime Values in Crystal Report using .NET 2005. Pin
SKSurya19-Aug-07 19:47
SKSurya19-Aug-07 19:47 
GeneralLoad Error Pin
Elizma12-Jun-07 19:06
Elizma12-Jun-07 19:06 
GeneralRe: Load Error Pin
Member 424163930-Apr-08 17:15
Member 424163930-Apr-08 17:15 
QuestionMultiple Page Navigation Problem Pin
aspass15-May-07 21:44
aspass15-May-07 21:44 
Questionplease hlep me Pin
creative2002guy8-May-07 22:54
creative2002guy8-May-07 22:54 
GeneralGood Article Pin
RZaitoun18-Apr-07 20:58
RZaitoun18-Apr-07 20:58 
GeneralWhen I Click on Print Button, Report Disappeared Pin
see_me_honey21-Mar-07 14:12
see_me_honey21-Mar-07 14:12 
Hey:
when i click on print button or any button report will disappear...
even when i try to zoom report e.g 200%, it will work but if i want to come back to original state example 100% report will remain in 200% Why?
any idea or suggestion.....
Waiting for reply

GeneralRe: When I Click on Print Button, Report Disappeared Pin
CrazyCoder2626-Jun-08 22:56
CrazyCoder2626-Jun-08 22:56 
GeneralNice, but i need your help Pin
Ahmed EL Gendy17-Mar-07 13:11
Ahmed EL Gendy17-Mar-07 13:11 
Questionwhat if i have 3 tables Pin
majidbhatti28-Jan-07 23:36
majidbhatti28-Jan-07 23:36 
GeneralGreat Article! Pin
Capitolxxx1-Jan-07 18:48
Capitolxxx1-Jan-07 18:48 
QuestionReportdocument Load/SetDataSource "Access is denied" exception error Pin
sma7778-Nov-06 11:34
sma7778-Nov-06 11:34 
Generalexcellent article Pin
dumin20062-Nov-06 19:26
dumin20062-Nov-06 19:26 
Generalgood article Pin
wildboy853-May-06 5:04
wildboy853-May-06 5:04 
GeneralCrystal Report Pin
Diamond1131-Mar-06 16:25
Diamond1131-Mar-06 16:25 

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.