Sunday, October 24, 2021

Crystal Report in Visual Studio 2010

 Crystal Reports has been excluded In VS2010. Yes, what you just heard is right. That means, if you install VS 2010 then you will not get Crystal Reports built-in. 

But don't worry it is available as a separate download from the SAP website. 
 
To download Crystal Reports, click on the following link:

Introduction

 
This is my first article. In this article, I will show you a simple Crystal Report creation process with screenshots using Visual Studio 2010. Because a picture is worth more than a thousand words, so I always believed in an article with screenshots is much better.
 
Let's start by creating a new website in VS 2010.
 
Open VS 2010, select Visual C# and ASP.NET Web Site, and click OK as shown below.
 
t1.gif
 
This action will create a new Web site project.
 
Once we have a Web site project created, the next step is to get database access to the project. That we do use a DataSet from a database.
 
Creation of Dataset (xsd) File
  • The following figure shows you the process to create a DataSet file.
  • To add a DataSet file, click on Solution Explorer -> Right Click on Project -> click on Add new Item, and then it will show you the following screen:
     
    t2.gif
     
  • Enter the Dataset file name. Click on the ok button.
     
    t3.gif
     
  • It will ask for confirmation to put that file in the App_Code folder. Just click yes and that file will be opened in the screen as a blank screen.
     
    t4.gif
     
  • Now we will add one blank data table to that mydataset.xsd.
  • Right-click in the area of the file and select Add -> Datatable.
  • It will add one DataTable1 to the screen.
  • The following Figure 5 shows how to add a data table to the mydataset.XSD file.
     
    t5.gif
     
  • Now, datatable1 is added to XSD file.
     
    t6.gif
     
  • Now we will add a data column to the datatable1 as per figure 6.
  • Remember, whatever columns we add here will be shown in the report.
  • So add the columns you want to display in your reports one by one here.
     
    t7.gif
     
  • Always remember to give the same name for the column and data type of column which is the same as the database, otherwise, you will get an error for field and data type mismatch.
     
    t8.gif
     
    t9.gif
     
  • To set the property for the columns the same as the database.
  • The following figure will show you how to set the property for the data columns.
  • The default data type for all the columns is a string.
  • To change the data type manually right-click on the datacolumn in the datatable and select property.
     
    t10.gif
     
  • From the property window, select the appropriate data type from the DataType Dropdown for the selected data column.
     
    t11.gif
     
  • XSD file creation has been done.
  • Now we will move on to create the Crystal Reports design.
Creation of Crystal report design
  • Click on the Solution Explorer -> Right-click on the project name and select Crystal Reports.
  • Name it as you choose and click the add button.
     
    t12.gif
     
  • After clicking on the add button a .rpt file will be added to the solution.
  • It will ask for the report creation type of how you want to create the report.
     
    t13.gif
     
  • Click the ok button to proceed.
     
    t14.gif
     
  • Under Data Sources, expand ADO.NET Datasets and select Table and add to the selected table portion located at the right side of the window using the > button. Click on Next.
     
    t15.gif
     
  • Select the columns that you want to show in the report.
  • Now click on the Finish button and it will show the next screen.
     
    t16.gif
     
  • Once the report file is added, you can see the Field Explorer on the left side of the screen.
  • Expand Database Fields, under that you will be able to find the Datatable that we have created earlier.
  • Just expand it and drag one by one each field from the Field Explorer to the rpt file in the under detail section.
  • Now the report design part is over.
  • Now we have to fetch the data from the database and bind it to the dataset and then Show that dataset to the report viewer.
Crystal Report Viewer
  • First Drag a CrystalReportViewer control on the aspx page from the Reporting Section of the toolbox.
  • Add a command Button.
     
    t17.gif
     
  • Configure the CrystalReportViewer and create a link with Crystal Reports.
  • Select the Crystal Reports source from the right side of the control.
     
    t18.gif
The following is the final code for reports (Default.aspx).
 
Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using CrystalDecisions.CrystalReports.Engine;  
  8. using CrystalDecisions.Shared;  
  9. using System.Data;  
  10. using System.Data.SqlClient;  
  11. using System.Configuration;  
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         CrystalReportViewer1.Visible = false;  
  17.     }  
  18.     protected void cmdcrystal_Click(object sender, EventArgs e)  
  19.     {  
  20.         CrystalReportViewer1.Visible = true;  
  21.         ReportDocument rDoc = new ReportDocument();  
  22.         Mydataset dset = new Mydataset(); // dataset file name  
  23.         DataTable dtable = new DataTable(); // data table name  
  24.         dtable.TableName = "Crystal Report ";  // Crystal Report Name  
  25.         rDoc.Load(Server.MapPath("mycrystal.rpt")); // Your .rpt file path  
  26.         rDoc.SetDataSource(dset); //set dataset to the report viewer.  
  27.         CrystalReportViewer1.ReportSource = rDoc;  
  28.     }  
  29. }
Output
 
output.gif
 
t20.gif
 

Summary

 
Crystal Reports template is not a part of Visual Studio 2010 anymore but you can download Crystal Reports support from the SAP website. In this article, I demonstrated step by step procedure of creating a Website project and adding Crystal Report support. After that, I created a DataSet from a database and in the end, built a report displaying data from the database.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...