Monday, February 21, 2011

WPF Crystal Report Viewer Using SAP

There is no doubt that we fall a great problem that the VS2010 is not intregated crystal report. Initially it seems to be a big problem. Hare is some step for SAP crystal report that we can use in our WPF application.

1.Download  Crystal report from this Link:


2. Remove Crystal report if any exist.

3. Close your VS-2010 and install the new downloaded CRforVS_13_0 .

4. Take a new WPF project




 
5. Right click on the project click on Properties



6. Change the target framework .NET Framework 4 Client Profile to  .NET Framework 4.



7. Click on main window then click on Toolbox.  Right Click on the General Tab then click on Choose Item.



8. It will appear this window click on WPF Component.



9.  Select CrystalReportsViewer  click on ok  Button.




10. Now you will see the report viewer control.




11. Your Crystal Report Environment is ready. Now we will add Crystal report. Maximum time we use dataset for report source But I Strongly recommend to use your Class as report data source.

12.  Right click on the solution add a new window name ReportViewerUI.




Drag & drop the CrystalReportsViewer Control from Toolbox in ReportViewerUI.  Wait till the window look like





13. Modify the XAML like :
 <Window x:Class="WPFReportTest.ReportViewerUI"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     Title="ReportViewerUI" Height="399" Width="724" xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer">  
   <Grid>  
     <my:CrystalReportsViewer Margin="63,38,0,0" Name="crystalReportsViewer" VerticalAlignment="Top" ShowLogo="False" HorizontalAlignment="Center"  
                  ShowToggleSidePanelButton="True" ShowToolbar="True" ShowOpenFileButton="False" />  
   </Grid>  
 </Window>  




and CS code:
 
 using System.Windows;  
 using System.Windows.Controls.Primitives;  
 namespace WPFReportTest  
 {  
   public partial class ReportViewerUI : Window  
   {  
     public ReportViewerUI()  
     {  
       InitializeComponent();  
       var sidepanel = crystalReportsViewer.FindName("btnToggleSidePanel") as ToggleButton;  
       if (sidepanel != null)  
       {  
         crystalReportsViewer.ViewChange += (x, y) => sidepanel.IsChecked = false;  
       }  
     }  
     public void setReportSource(CrystalDecisions.CrystalReports.Engine.ReportDocument aReport)  
     {  
       this.crystalReportsViewer.ViewerCore.ReportSource = aReport;  
     }  
   }  
 }  




 
14.You will get Error like this.



Right click on the project references add reference then add the 3 dll
Name: CrystalDecisions.CrystalReports.Design
      CrystalDecisions.CrystalReports.Engine
      CrystalDecisions.ReportSource
Now Build the Solution it will be Success.

15. Add a Report utility Class Name ReportUtility
  and modify the class with this method. 
 
 public static void Display_report(ReportClass rc, object objDataSource, Window parentWindow)  
     {  
       try  
       {  
         rc.SetDataSource(objDataSource);  
         ReportViewerUI  
               Viewer = new ReportViewerUI();  
         Viewer.setReportSource(rc);  
         Viewer.ShowDialog();  
       }  
       catch (Exception ex)  
       {  
         throw ex;  
       }  
     }  



16. Now we are want to show a table data in report. Let’s create a database ReportTestDB





Add a New table Like



Select Id as Primary Key and auto Increment. Give the name of the Table EMPLOYEE_INFO.

17. Now Insert some data




18. Right Click on the solution, Add new Item select LINQ to SQL DBML File name it ReportTestDataContext.dbml




19. It will appear this window  click on



Select your server name and database name

 
20. Expand the database select the table and drag and drop it to the DBML File.




Now Save it and close the window.
21. Add a New EEmployee Class which contain the following:

 public class EEmployee  
   {  
     public int Id { get; set; }  
     public string Name { get; set; }  
     public int Age { get; set; }  
     public string Address { get; set; }  
   }  


Also add 2 another class BEmployee and EmployeeDAL
 
 public class BEmployee  
   {  
     private EmployeeDAL employeeDALObj = new EmployeeDAL();  
     public List<EEmployee> GetAllEmployeeInfo()  
     {  
       return employeeDALObj.GetAllEmployeeInfo();  
     }  
   }  
 public class EmployeeDAL  
   {  
     private ReportTestDataContextDataContext reportDataContextObj = new ReportTestDataContextDataContext();  
     internal List<EEmployee> GetAllEmployeeInfo()  
     {  
       List<EEmployee> employees = new List<EEmployee>();  
       foreach (var info in reportDataContextObj.EMPLOYEE_INFOs)  
       {  
         EEmployee eEmployeeObj = new EEmployee();  
         eEmployeeObj.Id = info.Id;  
         eEmployeeObj.Name = info.Name;  
         eEmployeeObj.Age = (int) info.Age;  
         eEmployeeObj.Address = info.Address;  
         employees.Add(eEmployeeObj);  
       }  
       return employees;  
     }  
   }  


22. Right Click on the solution add a Crystal Report name EmployeeInfoCrystalReport




Select as a Blank report



Right click on the DataBase Field and click Database Expert



Expand  the .NET object and select EEmployee and click ok button.





Now Expand the Database field Drag and drop the attribute and design your crystal report.



After Completing your design Save all and close window.
23. Now In main window Take a Button Control  Name Show Employee Info . On the click event write down the code.

 List<EEmployee> employeeInfoList = bEmployeeObj.GetAllEmployeeInfo();  
       if (employeeInfoList.Count > 0)  
       {  
         EmployeeInfoCrystalReport employeeInfoCrystalReport = new EmployeeInfoCrystalReport();  
         ReportUtility.Display_report(employeeInfoCrystalReport, employeeInfoList, this);  
       }      
       else  
       {  
         MessageBox.Show("Don't have any records.", "Employee Info", MessageBoxButton.OK, MessageBoxImage.Information);  
       }  

25. Now Build the solution Run it Click on show Employee you Will get The following Error



To remove the error you have to add  this line of code in app.config file

 <startup useLegacyV2RuntimeActivationPolicy="true">  
       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  
    </startup>  

26. Now Build the solution Run it and you will get Your Desire report.



Download Crystal Report Source Code
Feel free for any comment.
thank you.