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
- Microsoft C#.NET 2005
- Windows 2000/2003/XP
- 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.
- Start a new ASP .NET Web site, Choose Location as "File System" and provide a path.
- Perform "Add New Item", choose "XML Schema".
Add elements as the picture below.
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".
- Give a name for the report.
- Choose -- Using the Report Wizard. -- OK.
- A Data window will appear, Click on "Create New Connection" and then "ADO .NET".
- A connection windows will appear. Provide the XML Schema file that u have created just now. Finish.
- The Schema will appear under the "ADO .NET" tag. Choose. And select using > button.
- Click Next. Fields windows will appear. Select all using >> button.
- 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;
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.