Saturday, February 16, 2013

Crystal Report in Silverlight 5


Long time ago, Visual Basic and Visual Studio have crystal report many version with it. People are saying that since 1993. Crystal Report is available as a separate download from the SAP web site. Crystal Reports for Visual Studio 2010 will continue to be free, with no registration required. You can directly download crystal report exe file from here.


Introduction:

In this article, I will show you simple report creation process with screenshots. A picture is worth more than a thousand words, so I always believe in an article with screenshots. Here I will explain some system to view crystal report. I am going to run the below 5 systems.

  1.  Simple Crystal Report by Using ADO.NET with data set.
  2.  Generate Crystal Report with data set.
  3.  Generate Crystal Report with multiple data table in data sets.
  4. Generate Crystal Report by XML

Let's start by creating a new website in VS2010. See the following screen:
As per figure 1 and 2, create a new Silverlight application in VS2010 and name it as per your choice. Now I am going to discuss data base structure below

   Figure 1

Press OK Button to Create a New Project Name 'CrystalArticle'

    Figure 2

Firstly you have to create a data base table that is name you may choose any thing. The data base table looks like this as figure 3 or you may down load script from my uploaded script file.


   Figure 3

Now you can write some data into table by this script or manually like as figure 4

    Figure 4


But in the attached project, I am getting data from the XML file. I have saved all the data from table to XML. So you can easily use the attached project. Next Dataset (xsd file) creation.
There are so many ways to pass the data to Crystal Report. Here I am using Dataset (xsd file) to bind Crystal Report. In my point of view, this is the best way to bind. So, next step is Dataset (xsd file) creation.
Go to CrystalArticle.Web->ClientBin -> Right click on the Project -> Add New Item. Figure 5 shows the Dataset creation.
   Figure 5
Once you click on Add button, it will ask one confirmation message. Just click yes. Now you can see thedsTestCrystal.xsd dataset file opened in Visual Studio. Here you need to create one Data Table with all the columns names you need to shown in Crystal Report. To Create Data Table, see the below Figure 6.
    Figure 6
Now you can see Data Table created in the Dataset. Next, we need to add the columns names need to shown inCrystal Report. Please note that this Data Table column names and data types should be the same as your table in database. If you are getting any data type property mismatching error, just think that this is the problem. To Create Column names, see the below Figure 7.

    Figure 7
Add all the columns one by one and set the correct data type. Here by default, data type is string. If any columns have different data type, we need to change manually. In this project, I am using ‘Product_Qty’ column data type as Decimal. So to change the data type, see the below Figure 8.
    Figure 8
Dataset (dsTestCrystal.xsd) creation has been done. Now we need to design Crystal Report.
That's it. XSD file creation has been done. Now we will move to create Crystal report design.
Just click on the Solution Explorer -> Right click on the project name and select crystal reports. Name it as per your choice and hit the add button.
Figure 9 will show you the creation process of Crystal reports.

Figure 9
Click on the add button and one .rpt file will be added to the solution. And also, it will ask for the report creation type of how you want to create the report. Figure 10 will show you a screenshot.

Figure 10
Just click ok button to proceed. It will lead you to figure 11:



    Figure 11
Under project data, expand ADO.NET Datasets and select DataTable1 and add to the selected table portion located at the right side of the windows using > button.
Now click on the Finish button and it will show the next screen (Figure 12):


   Figure 12
Once report file is added, you can see Field Explorer on the left side near server explorer.
Expand Database Fields, under that you will be able to find Datatable that we have created earlier. Just expand it and drag one by one filed from Field Explorer to the rpt file under detail section.
Now the report design part is over. Now we have to fetch the data from database and bind it to dataset and then bind that dataset to the report viewer.
Let's go step by step.
First Drag a CrystalReportViewer control on aspx page from tool box as per below screen:


    Figure 13
Now we will fetch the data, pass data to the dataset and then add that dataset to the Crystal Report. Now you have to create a single aspx page like Report.aspx
    Figure 14
Below is the C# code which will do the job into Report.aspx page code behind:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument rptDoc = new ReportDocument();
    dsSample ds = new dsSample();
    DataTable dt = new DataTable();

   
    dt.TableName = "Crystal Report Example";
    dt = getAllOrders(); 
    ds.Tables[0].Merge(dt);
 
    rptDoc.Load(ReportName));
  
    rptDoc.SetDataSource(ds);
    CrystalReportViewer1.ReportSource = rptDoc;
}

You can see the report by silverlight button click and you paste the code below into button click
var filePath   "http://" + Application.Current.Host.Source.Host + ":" + Application.Current.Host.Source.Port + "/ClientBin/Report.aspx";
Uri myURI = new Uri(HtmlPage.Document.DocumentUri, filePath);HtmlPage.Window.Navigate(myURI);

Thank you.

1 comment: