using crystal reports in asp.net

34
An Easy Approach to Get Expertise in ASP.Net with MS visual C-Sharp Our Aim: Spread Knowledge with every single bit 1 Visual Training Of Microsoft Visual Studio2005 Web based Applications using ASP.Net with MS Visual C-Sharp

Upload: zohair-ahmed

Post on 25-Nov-2015

33 views

Category:

Documents


5 download

DESCRIPTION

Crystal Reports in ASP.net

TRANSCRIPT

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

    Our Aim: Spread Knowledge with every single bit 1

    Visual Training

    Of

    Microsoft Visual Studio2005

    Web based Applications using ASP.Net with MS Visual

    C-Sharp

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

    Our Aim: Spread Knowledge with every single bit 2

    Learning Crystal Reports Step by Step

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

    Our Aim: Spread Knowledge with every single bit 3

    Day 8 In todays session:

    9 Why reports are needed for the Business Success 9 Crystal Report 9 Creating First Crystal Report 9 Attaching Report to a web form

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

    Our Aim: Spread Knowledge with every single bit 4

    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.

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

    Our Aim: Spread Knowledge with every single bit 5

    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,

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

    Our Aim: Spread Knowledge with every single bit 6

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

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

    Our Aim: Spread Knowledge with every single bit 7

    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.

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

    Our Aim: Spread Knowledge with every single bit 8

    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,

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

    Our Aim: Spread Knowledge with every single bit 9

    Save it,

    View_Employee_Details

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

    Our Aim: Spread Knowledge with every single bit 10

    2. Creating a Report based on this View or SPS

    Add a folder for the Reports in your application

    Rename the folder to Reports

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

    Our Aim: Spread Knowledge with every single bit 11

    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

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

    Our Aim: Spread Knowledge with every single bit 12

    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

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

    Our Aim: Spread Knowledge with every single bit 13

    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

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

    Our Aim: Spread Knowledge with every single bit 14

    o User SID o Password o Select the Database Name

    Press the Next Button

    Press finish, this will show the dialogue,

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

    Our Aim: Spread Knowledge with every single bit 15

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

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

    Our Aim: Spread Knowledge with every single bit 16

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

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

    Our Aim: Spread Knowledge with every single bit 17

    Select it and add it to other List Box

    Press Next,

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

    Our Aim: Spread Knowledge with every single bit 18

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

    press Next btton

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

    Our Aim: Spread Knowledge with every single bit 19

    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

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

    Our Aim: Spread Knowledge with every single bit 20

    You can choose different format from these available formats,

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

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

    Our Aim: Spread Knowledge with every single bit 21

    The report created is shown here,

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

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

    Our Aim: Spread Knowledge with every single bit 22

    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

  • 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

    Our Aim: Spread Knowledge with every single bit 24

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

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

    Our Aim: Spread Knowledge with every single bit 25

    Select the required Format and then press OK button\

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

    Our Aim: Spread Knowledge with every single bit 26

    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.

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

    Our Aim: Spread Knowledge with every single bit 27

    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,

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

    Our Aim: Spread Knowledge with every single bit 28

    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

  • 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

    Our Aim: Spread Knowledge with every single bit 30

    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_Details.rpt"));

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

    Our Aim: Spread Knowledge with every single bit 31

    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_Details.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

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

    Our Aim: Spread Knowledge with every single bit 32

    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(); } }

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

    Our Aim: Spread Knowledge with every single bit 33

    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,

    Add inside the following line before the closing tag So that the should look like as

    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.

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

    Our Aim: Spread Knowledge with every single bit 34

    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

    [email protected]

    +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.