Tuesday 6 November 2012

Crystal Reports in vs 2010


This tutorial focuses on creating a crystal report in Visual studio 2010. The report uses a stored procedure to get its data from an Sql server 2008 R2 database. In order to create the report you will need to download and install crystal reports for Visual Studio 2010. This is because VS 2010 does not ship with crystal reports. However you can download crystal reports from SAP websitehttp://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0.exe
  1. Start by running the following scripts in sql database to create a test table, insert some records and also create a procedure that will retrieve the records.
    --===========================================
    --create a test customer table
    CREATE TABLE t_Customers
    (
        CustomerID VARCHAR(20),
        FirstName VARCHAR(25),
        LastName VARCHAR(25)
    )
    --===========================================
    --insert dummy data into the test table
    INSERT INTO t_Customers(CustomerID,FirstName,LastName)
    VALUES('12345','Evans','Amenya')
    INSERT INTO t_Customers(CustomerID,FirstName,LastName)
    VALUES('324132123','ASas','ASasASas')
    INSERT INTO t_Customers(CustomerID,FirstName,LastName)
    VALUES('234234','evans','asdasdasd')
    --===========================================
    --create procedure to retrieve the records
    CREATE PROCEDURE sp_getCustomers
    AS
        SELECT CustomerID,FirstName,LastName FROM t_Customers
    --=========================================== 
  2. Create a new Visual Studio 2010 project
  3. Right-click on the project on the solution explorer and click on Add New Item on the pop-up menu as shown below
    adding a new item in visual studio 2010
  4. The Add New Item dialogue box will appear as shown below. Select Crystal Reports in the middle pane then enter the name as “CustomersReport.rpt” in the Name field.
    selecting crystal report in add item dialogue box
  5. Click the Add button.
    Crystal Reports Gallery dialogue box will appear as shown below
    Crystal Reports Gallery
    Select As a Blank Report in Create a New Crystal Report Document options. Then click the OK button
  6. A blank report will appear. Right-click on any blank area on the report, then point to Database on the pop-up menu that appears. Click on Database Expert as shown below
    right-click on blank database image
  7. The Database Expert dialogue box will appear as shown below. Expand the Create New Connection node. Then expand the OLE DB (ADO) node.
    Creating a connection to database
  8. OLE DB Provider dialogue box will appear as shown below. Scroll down and select SQL Server Native Client 10.0 from the list of Providers. Then click the Next button.
    OLE DB (ADO) - OLE DB Provider dialogue box
  9. OLE DB Connection information dialogue box will appear as shown below. Provide necessary information to log on and click Next.
    OLE DB (ADO) - Connection Information dialogue box
  10. The Advanced Information dialogue box will appear as shown below. Click the Finish button.
    OLE DB (ADO) - Advanced Information dialogue box
  11. The connected database will appear as shown in the screen below. Expand the Database name node.
    Expand the dbo node. Expand the Stored Procedures node. Select the stored procedure to use,
    “sp_getCustomers”, then click the right-pointing single arrow to move the procedure to the right-hand pane.
    Database Expert - Data dialogue box
  12. Click OK button.
    The Database Expert dialogue box closes and the blank report is visible.
    Expand the Database Fields node on the Field Explorer box. Expand procedure name node ("sp_getCustomers") as shown below.
    Field explorer box
  13. Drag and drop the fields from the Field Explorer box onto the blank report just below the Details
    section. After you release the mouse key, the heading should automatically appear as shown below.
    Drag fields from field explorer and drop on report designer
  14. The linking of our crystal report to sql server database is complete. Now we can create a .aspx page that will call it.
    Add web form to the project and choose C# as the programming language. Also make sure the option to place code in separate file is selected. Name the page "CustomerReportViewer.aspx"
  15. Drag and drop a CrystalReportViewer control from the Reporting tab onto the CustomerReportViewer.aspx page. Your page should appear as shown below,
    CrystalReportViewer control on a page
  16. Add the following code on the code file
    ...
    using System.Reflection;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;
    using CrystalDecisions.Reporting;
    using CrystalDecisions;
    using System.Data.SqlClient;
    public partial class CustomerReportViewer : System.Web.UI.Page
    {
        ReportDocument doc;
        protected void Page_Unload(object sender, EventArgs e)
        {
            if (doc != null)
                doc.Close();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowReport(Server.MapPath(".\\CustomersReport.rpt"), "sp_getCustomers");
        }
        protected void ShowReport(String fileName, String strProcedureName)
        {
            TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            CrystalDecisions.CrystalReports.Engine.Database crDatabase;
            CrystalDecisions.CrystalReports.Engine.Tables crTables;
            doc = new ReportDocument();
            doc.Load(fileName);
            crConnectionInfo.ServerName = "HP-PC";
            crConnectionInfo.DatabaseName = "ProgrammingSamples";
            crConnectionInfo.UserID = "xxxxxxx";
            crConnectionInfo.Password = "yyyyyyyy";
            crConnectionInfo.Type = ConnectionInfoType.SQL;
            crConnectionInfo.IntegratedSecurity = false;
            crDatabase = doc.Database;
            crTables = crDatabase.Tables;
            SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand("dbo." + strProcedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adpt.Fill(dataSet, "Customers");
            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
            {
                crTableLogOnInfo = crTable.LogOnInfo;
                crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
                crTable.ApplyLogOnInfo(crTableLogOnInfo);
            }
            doc.SetDataSource(dataSet.Tables[0]);
            CrystalReportViewer1.ReportSource = doc;
        }
  17. Set "CustomerReportViewer.aspx" as the startup page and run the project.
    The report will be displayed as shown below.Crystal report displaying records from database

Download the complete source code for the example project


No comments:

Post a Comment

What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...