Tuesday, September 13, 2011

Creating Reports in C# using Crystal Reports with Xml data Definitions


This article explains how to extract data into a Crystal Report created outside a C# project using xml data definitions and data sets.

In this project,
  • The report is formatted outside the project via Crystal Reports (the .rpt file)
  • The data to the report is extracted from a field definition file created as a .xsd extension (as compatible with ADO.Net)
  • The report is called and displayed through a form using the crystal report viewer.
We have taken the Authors table in the pubs database where au_id, au_lname, au_fname will be printed on the report.

The main requirement is to create the Xml schema file with .xsd extension and bind the data to the .rpt file.

The example handles this in one form with two buttons: The XSD Button to create the .xsd field definition file and the VIEW button to generate the report.

So here is the step by step procedure to arrive at this.

1. Create the .xsd field definitions file depending on the data that we want to extract from a database.

Insert the following code in the click event of XSD button, to create the .xsd file. In our example this file is called the 'sampledatadef.xsd'. 


#region private void SetSource()
        private void
SetSource()
        {
            rptReceipt rpt = new rptReceipt();           
            Receipt dsReceipt = new Receipt();               
            DataTable dtReceipt = new DataTable();
                   
            dtReceipt = dsReceipt.dtReceipt;
                    
//LINQ - SQL datasource
            List source= Rules.Receipt.GetReceiptByCustomerId(custId);


            foreach (Receipt_SelectResult item in source)
            {
                DataRow drReceipt = dtReceipt.NewRow();
                drReceipt["Order_No"] = item.Order_No;
                drReceipt["Status"] = item.Status;
                drReceipt["Cust_Name"] = item.Status;
                drReceipt["Phone_No"] = item.Phone_No;
                drReceipt["Booking_Date"] = item.Booking_Date;
                drReceipt["Delivery_Date"] = item.Delivery_Date;
                drReceipt["Advance"] = item.Advance;
                drReceipt["Balance"] = item.Balance;
                drReceipt["Refund"] = item.Refund;
                drReceipt["SlNo"] = item.SlNo;
                drReceipt["Details"] = item.Details;
                drReceipt["Quantity"] = item.Quantity;
                drReceipt["Rate"] = item.Rate;
                drReceipt["Category"] = item.Category;
                drReceipt["Desc"] = item.Desc;
                dtReceipt.Rows.Add(drReceipt);
            }

            rpt.SetDataSource(dsReceipt);
            crvPrintPreview.ReportSource = rpt;
        }          
        #endregion 
   

2. Now create the .rpt file in crystal reports using the .xsd field definitions file.

You need to select 'Create new Connections' and take option - Field Defintions. Select the ADO.NET option. In the dialogue box you will be asked to enter the xsd file name. Select the path and click the 'Finish' button. 

We have named this report as crystalsample.rpt (saved in c:\)

If the ADO.Net option is not available, you will have to add new components to the installed crystal reports using 'Add/Remove' programs option and include the necessary component to the Crystal Reports installation.


3. Generate the class to hold the data set for the .xsd datadefiniton
 
This is an important step. The .xsd definitions do not recognize the datasets generated through the normal DataSet class. The report will be displayed only with the headings, if this step is not accomplished!!! (This was a weird experience to us).

This is done though executing the following command through the command prompt tool of the Visual Studio .Net Tools.
xsd.exe /d /l:C# sampledatadef.xsd 

(The /d directive tells the tool to generate DataSets, /l specifies the language to use)

This command generates the DataSet class compatible with the .xsd file you created in the path in which you executed the command. The name of the source file will be sampledatadef.cs.

 4. Now add this class to your project.In the source file, the name of the class will be generated as 'NewDataSet'. Rename this name to a name that you desire in the source file. We have named it as ds_SampleDataSet.

5. Now generate the code to extract the data from field definition
In our example we have used a new form for this, which is loaded with the click event of the VIEW button.
This is done in an easy set of steps.
  1. Define a dataset of the newly generated type. In this example
    Private ds_sampledataset mydataset = New ds_sampledataset() 
  2. Fill this dataset with the identical data fields that you created for the xsd definition file. In this example they are au_id, au_lname, au_fname of Authors.  
  3. Open a new ReportDocument type class and call the .rpt file you created into it. (Make sure that the necessary references are added to the project - CrystalDecisions.CrystalReports.Engine CrystalDecisions.Shared )
 

No comments:

Post a Comment