Skip to main content

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.

Comments

  1. I can't download the source code.

    I have developed the report in wpf with step by step but Group Tree will display on the screen.

    If any suggestion please let me know.

    Brij

    ReplyDelete
  2. I can`t download, url is break.

    ReplyDelete
  3. Source code download is not working, please reupload in mediafire

    ReplyDelete
  4. Please download from this link:

    https://skydrive.live.com/?sc=documents&cid=dbc6b335a854cc0e#cid=DBC6B335A854CC0E&id=DBC6B335A854CC0E%21113

    ReplyDelete
  5. Download the Latest Source Code

    http://code.msdn.microsoft.com/WPF-SAP-Crystal-report-e9c878b8

    ReplyDelete
  6. Very very thanks brother for your tutorial. No better tutorial exists in the internet than this. But i need some further help brother. I want to make a customer receipt using crystal report and wpf and also want to search by date on a crystal report. I need your help. Thanks in advance.

    ReplyDelete
  7. You are most welcome. On date range search query set 2 date picker on your UI & made the list parametrize which is used for the report source.

    ReplyDelete
  8. i have do what you have done completely same but i got an exception on reportutility class.
    Object reference not set to an instance of an object.

    please help

    ReplyDelete
  9. Hi Aimanzaki,

    Check the method "Display_report".

    ReplyDelete
  10. Thanks for your help suggestion brother but as i am a newcomer i am not understanding, if you make another wpf tutorial on parameter based reporting, i mean extending your current tutorial to parameter based such as year by search and employee search that would be very helpful for us. Thanks once again brother for replying.

    ReplyDelete

Post a Comment

Popular posts from this blog

C# run powershell script as administrator

Recently I was fetching a problem that I need to run a PowerShell script that will change TFS user Display name and SID. I was trying to run that script from C# that was not working due to TFS security update and TLS certificate. Using this code block I resolve the Issue. var newProcessInfo = new System.Diagnostics.ProcessStartInfo(); newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"; newProcessInfo.Verb = "runas"; // Define Run as administrator newProcessInfo.Arguments = script; //Define your powershell script newProcessInfo.UseShellExecute = false; newProcessInfo.RedirectStandardOutput = true; // This will enable to read Powershell run output newProcessInfo.RedirectStandardError = true; Process proces = System.Diagnostics.Process.Start(newProcessInfo); proces.WaitForExit(); // I want to read the output string from powershell window StringBuilder output = new StringBuilder(); output.Append("Started"); while (!proces.St

ASP.NET MVC razor SAP Crystal report

Crete a new project: Add a aspx Master Page Create a new folder Reports and 2 sub folder crystal & crystalviewer Now add a web form page in crystalviewer  folder. Add the master page namespace in your web form page. MasterPageFile ="~/Views/Shared/ReportSite.Master" Replace your web form by this code < asp : Content ID ="Content1" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="server">      </ asp : Content > Now go to design mode of your web form drag & drop the crystal report viewer in your web form. After that your page will be look look like this. Replace the code: < CR : CrystalReportViewer ID ="EmployeeList" runat ="server"   HasCrystalLogo ="False"     AutoDataBind ="True"   Height ="50px"   EnableParameterPrompt ="false" EnableDatabaseLogonPrompt

mvc razor textboxfor change event change another textboxfor value

Based on value of Weight, Rate , CNF & AWB it will change the value of Freight , TTLCNF anfd TTLFright . Freight= Weight*Rate; TTLCNF  = Weight*CNF; TTLFright=  Freight+ TTLCNF  + AWB; @Html.TextBoxFor(model => model.Weight, new { onChange="return GetWight(this);"}) @Html.TextBoxFor(model => model.Rate, new { onChange="return GetWight(this);"})/Kg @Html.TextBoxFor(model => model.Freight, new {disabled = "disabled" , @readonly = "readonly" ,onChange="return GetTTLFright(this);"}) @Html.TextBoxFor(model => model.CNFPK, new { onChange="return GetCNFPK(this);"}) @Html.TextBoxFor(model => model.TTLCNF, new {disabled = "disabled" , @readonly = "readonly",onChange="return GetTTLFright(this);" }) @Html.TextBoxFor(model => model.AWB, new { onChange="return GetTTLFright(this);"}) and script <script> function GetW