Click here to Skip to main content
11,577,258 members (64,647 online)
Click here to Skip to main content

Developing Web Report using Crystal Report in .NET 2005.

, 29 Mar 2006 156.8K 2.3K 52
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Suranjan Nandi
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.

You may also be interested in...

Comments and Discussions

 
QuestionCrystalReport 8.5 Based on Queryfile in .net 2.0 Pin
anand45612-Dec-11 0:14
memberanand45612-Dec-11 0:14 
GeneralMy vote of 1 Pin
db conner13-Apr-10 16:59
memberdb conner13-Apr-10 16:59 
GeneralMy vote of 1 Pin
shankar.koppella24-Feb-10 22:40
membershankar.koppella24-Feb-10 22:40 
GeneralMy vote of 1 Pin
DungVinh11-Dec-09 19:23
memberDungVinh11-Dec-09 19:23 
GeneralGreat Article Pin
briogene19-Feb-09 2:26
memberbriogene19-Feb-09 2:26 
QuestionAdditional window appearing while clicking on print button of crystal report Pin
Manoj Tillu25-Aug-08 23:12
memberManoj Tillu25-Aug-08 23:12 
GeneralGreat article Pin
ssavkin11-May-08 3:26
memberssavkin11-May-08 3:26 
Generalxml schema Pin
senorita312825-Apr-08 4:56
membersenorita312825-Apr-08 4:56 
QuestionTrying to show the correct output using two tables Pin
charm_depths_274-Sep-07 20:51
membercharm_depths_274-Sep-07 20:51 
GeneralUnable To Display MySQL DateTime Values in Crystal Report using .NET 2005. Pin
SKSurya19-Aug-07 19:47
memberSKSurya19-Aug-07 19:47 
GeneralLoad Error Pin
Elizma12-Jun-07 19:06
memberElizma12-Jun-07 19:06 
GeneralRe: Load Error Pin
Member 424163930-Apr-08 17:15
memberMember 424163930-Apr-08 17:15 
QuestionMultiple Page Navigation Problem Pin
aspass15-May-07 21:44
memberaspass15-May-07 21:44 
Questionplease hlep me Pin
creative2002guy8-May-07 22:54
membercreative2002guy8-May-07 22:54 
GeneralGood Article Pin
The Lord18-Apr-07 20:58
memberThe Lord18-Apr-07 20:58 
GeneralWhen I Click on Print Button, Report Disappeared Pin
see_me_honey21-Mar-07 14:12
membersee_me_honey21-Mar-07 14:12 
GeneralRe: When I Click on Print Button, Report Disappeared Pin
ARINDAM198126-Jun-08 22:56
memberARINDAM198126-Jun-08 22:56 
GeneralNice, but i need your help Pin
Ahmed EL Gendy17-Mar-07 13:11
memberAhmed EL Gendy17-Mar-07 13:11 
Questionwhat if i have 3 tables Pin
majidbhatti28-Jan-07 23:36
membermajidbhatti28-Jan-07 23:36 
GeneralGreat Article! Pin
Capitolxxx1-Jan-07 18:48
memberCapitolxxx1-Jan-07 18:48 
QuestionReportdocument Load/SetDataSource "Access is denied" exception error Pin
sma7778-Nov-06 11:34
membersma7778-Nov-06 11:34 
Generalexcellent article Pin
dumin20062-Nov-06 19:26
memberdumin20062-Nov-06 19:26 
Generalgood article Pin
wildboy853-May-06 5:04
memberwildboy853-May-06 5:04 
GeneralCrystal Report Pin
Diamond1131-Mar-06 16:25
memberDiamond1131-Mar-06 16:25 
AnswerRe: Crystal Report Pin
Suranjan Nandi4-Apr-06 18:52
memberSuranjan Nandi4-Apr-06 18:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 29 Mar 2006
Article Copyright 2006 by Suranjan Nandi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid