Sei sulla pagina 1di 34

An Easy Approach to Get Expertise in ASP.

Net with MS visual C-Sharp

Visual Training

Of

Microsoft Visual Studio2005

Web based Applications using ASP.Net with MS Visual C-Sharp

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Learning Crystal Reports Step by Step

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Day 8
In todays session: Why reports are needed for the Business Success Crystal Report Creating First Crystal Report Attaching Report to a web form

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Creating and Using the Crystal Reports in ASP.Net Web Site


The reports are the most important component of any application, because the management does not need the web site pages, they require (if the web application is about the Inventory): How much inventory is present in the stock How much inventory is sold out this month/week/day How much profit is obtained and from where store The details of the stock items

So, the reports are used for this purpose. A very useful reporting tool is available with the Visual Studio 2005. This tool is freely available in it. It is called Crystal Report.

A Report can be developed based on any of the following database objects: Table Views Stored Procedures (SPS) Lets see how to design and develop these reports and how to incorporate these reports into our web applications. For this we have to do the following tasks:

1. Creating View or Stored Procedure on which our Report will be based 2. Creating a Report based on this View or SPS 3. Designing a Form that will display the designed/developed report 4. Modifying the web.config file 5. Running the Report

Here, we create our crystal report from a View: the steps are described below, A view is logical table, which can be used to pull the data from the multiple tables.

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp


1. Creating View or Stored Procedure on which our Report will be based

Suppose we have the following table structure, the table name is tblEmployee tblEmployee (EmployeeSID, EmployeeName, FatherName, Gender, QualificationSID, DateOfBirth) & tblQualification

The QualificationSID is a foreign Key and its values is determined from the tblQualificatin, and its structure is show here

Create the View as shown here,

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Now, you will see the dialogue listing all of your database tables,

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Add the tblEmployee and tblQualifiaction tables one by one for your view and press the Close button. This will provide the following output:

The two tables are connected as you have defined the Primary-Foreign Key relationship between these two tables. Similar to tblQualification, you add other tables e.g. tblReligion, tblSect, tblCaster, tblDepartment etc for more detailed report.
Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Now, Right Click the line between the two tables and select the option as shown,

This will ensure all of the employee data will be pulled out, although their qualification details have not be entered. Select the column, that is required for the Report

Press the Execute Button, the result of the View will be displayed, as shown,

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Save it,

View_Employee_Details

Our Aim: Spread Knowledge with every single bit

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

2. Creating a Report based on this View or SPS

Add a folder for the Reports in your application

Rename the folder to Reports

Our Aim: Spread Knowledge with every single bit

10

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Right Click the newly added folder and choose Add New Item

Select Crystal Report, change its name to crp_Employee_Details.rp

Press Add button You will see the dialogue, if you are using Crystal Report for the first time, Check I accept the Agreement and press OK
Our Aim: Spread Knowledge with every single bit

11

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Then you will see different options available for creating Report, leave the default setting and press OK button

Expand the Create New Connection Node and then expand the OLEDB (ADO) option as shown

Our Aim: Spread Knowledge with every single bit

12

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

You will get the Window dialogue, select the option highlighted below

Microsoft OLEDB Provider for Microsoft SQL Server and press Next Enter SQL Server credentials and press Next button o Server Name

Our Aim: Spread Knowledge with every single bit

13

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

o User SID o Password o Select the Database Name

Press the Next Button

Press finish, this will show the dialogue,

Our Aim: Spread Knowledge with every single bit

14

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

You have noticed that HRSystem Database available there, or what so ever database you have chosen

Our Aim: Spread Knowledge with every single bit

15

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Expand the HRSystem database and then expand the View Node and choose your required View_Employee_Details as shown,

Our Aim: Spread Knowledge with every single bit

16

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Select it and add it to other List Box

Press Next,

Our Aim: Spread Knowledge with every single bit

17

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Expand the Node, choose column you want to be used in your report and then press Next btton

Our Aim: Spread Knowledge with every single bit

18

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

You can group your report data here, but now press Next button

Here, you can add or filter your report data, leave as it is and press Next button

Our Aim: Spread Knowledge with every single bit

19

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

You can choose different format from these available formats,

Choose the format according to your requirements and press the Finish button.

Our Aim: Spread Knowledge with every single bit

20

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

The report created is shown here,

At the bottom just click the Main Report View to view the reports result

Our Aim: Spread Knowledge with every single bit

21

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Now, formatting the report is shown here delete the EmployeeSID column as we do not require it o Creating a Serial Number
Right click the Report and choose the option as shown here, Insert Special Field Record No

Our Aim: Spread Knowledge with every single bit

22

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Our Aim: Spread Knowledge with every single bit

23

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Formatting the Date Column, select the date column and right click and then

Our Aim: Spread Knowledge with every single bit

24

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Select the required Format and then press OK button\

Our Aim: Spread Knowledge with every single bit

25

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Then press OK button, the result is as shown figure.

Further, you can format the report as you required, you can use the Box to encircle the contents.

Our Aim: Spread Knowledge with every single bit

26

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp


3. Designing a Form that will display the designed/developed report

Now, displaying this report is in web form. For this we add a new form.

Add a Crystal Report Viewer to the form as shown,

Our Aim: Spread Knowledge with every single bit

27

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Add this directive, as shown


using CrystalDecisions.CrystalReports.Engine;

Then, add the line in the code


private ReportDocument myReportDocument;

We have fix code to display any crystal report on ASP.Net web page, which is shown here

Our Aim: Spread Knowledge with every single bit

28

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Our Aim: Spread Knowledge with every single bit

29

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp


using CrystalDecisions.CrystalReports.Engine; public partial class Display_Report : System.Web.UI.Page { private ReportDocument myReportDocument; protected void Page_Load(object sender, EventArgs e) { ReportDisplay(); } protected void ReportDisplay() { CrystalDecisions.Shared.ConnectionInfo Conn = new CrystalDecisions.Shared.ConnectionInfo(); CrystalDecisions.CrystalReports.Engine.Table myTable; CrystalDecisions.Shared.TableLogOnInfo myLog; Conn.ServerName = ConfigurationManager.AppSettings["ServerName"]; //Server Name Conn.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"]; //DataBase Name Conn.UserID = ConfigurationManager.AppSettings["UserID"]; //User Name Conn.Password = ConfigurationManager.AppSettings["Password"]; //Password for (int iLog = 0; iLog < myReportDocument.Database.Tables.Count; iLog++) { myTable = myReportDocument.Database.Tables[iLog]; myLog = myTable.LogOnInfo; myLog.ConnectionInfo = Conn; myTable.ApplyLogOnInfo(myLog); myTable.Location = myLog.TableName; } // ------ Bind Report -----CrystalReportViewer1.ReportSource = myReportDocument; CrystalReportViewer1.DataBind(); } }

Now, we have to provide the name of the Report that we want to display. Add this line in the Report_Display() Event after the line
myReportDocument = new ReportDocument(); myReportDocument.Load(Server.MapPath("Reports/crp_Employee_Detail s.rpt"));

Our Aim: Spread Knowledge with every single bit

30

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

Final code is given here, with the report name


using CrystalDecisions.CrystalReports.Engine; public partial class Display_Report : System.Web.UI.Page { private ReportDocument myReportDocument; protected void Page_Load(object sender, EventArgs e) { ReportDisplay(); } protected void ReportDisplay() { myReportDocument = new ReportDocument();

myReportDocument.Load(Server.MapPath("Reports/crp_Employee_Detail s.rpt"));

CrystalDecisions.Shared.ConnectionInfo Conn = new CrystalDecisions.Shared.ConnectionInfo(); CrystalDecisions.CrystalReports.Engine.Table myTable; CrystalDecisions.Shared.TableLogOnInfo myLog; Conn.ServerName = ConfigurationManager.AppSettings["ServerName"]; //Server Name Conn.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"]; //DataBase Name Conn.UserID = ConfigurationManager.AppSettings["UserID"]; //User Name Conn.Password = ConfigurationManager.AppSettings["Password"]; //Password

Our Aim: Spread Knowledge with every single bit

31

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

for (int iLog = 0; iLog < myReportDocument.Database.Tables.Count; iLog++) { myTable = myReportDocument.Database.Tables[iLog]; myLog = myTable.LogOnInfo; myLog.ConnectionInfo = Conn; myTable.ApplyLogOnInfo(myLog); myTable.Location = myLog.TableName; } // ------ Bind Report -----CrystalReportViewer1.ReportSource = myReportDocument; CrystalReportViewer1.DataBind(); } }

Our Aim: Spread Knowledge with every single bit

32

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp


4. Modifying the web.config file

The steps involved in creating a crystal report are as follow: Find the section, shown below in the web.config file,
<appSettings> <add key="CrystalImageCleaner-AutoStart" value="true" /> <add key="CrystalImageCleaner-Sleep" value="60000" /> <add key="CrystalImageCleaner-Age" value="120000" /> </appSettings> Add inside the following line before the closing tag</appSetting> So that the <appSettings> should look like as <appSettings> <add key="ServerName" value="Saif" /> <add key="DatabaseName" value="HRSystem" /> <add key="UserID" value="sa" /> <add key="Password" value="rehman" /> <add key="CrystalImageCleaner-AutoStart" value="true" /> <add key="CrystalImageCleaner-Sleep" value="60000" /> <add key="CrystalImageCleaner-Age" value="120000" /> </appSettings>

Save all
Set the Report Page as Start page and run the site

Thats all from the todays lecture. Further, you can explore the Crystal reports as yourequire.

Our Aim: Spread Knowledge with every single bit

33

An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp

These lecture slides are prepared and compiled by: Saif Ur Rehman Saifi PhD (Scholar), Muhammad Ali Jinnah University, Islamabad MS.Net Trainer, Buraq IT School and IMIT, Rawalpindi Saifi.ur.rehman@gmail.com

+92-343-580-2355

http://www.alrehmanitl.com We will be pleased, if any of you share this file with other of your friends who want to learn MS Visual Studio, MS ASP.Net, MS C-Sharp, MS Visual Basic.Net, MS SQL Server2005, and Crystal Reports in easy steps.

If you have any query regarding this document or any other question related to MS.Net then we will well come all of you. Any Suggestion, comments are well come to improve this document.

Our Aim: Spread Knowledge with every single bit

34

Potrebbero piacerti anche