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.
Download Crystal Report Source Code
Feel free for any comment.
thank you.
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.
I can't download the source code.
ReplyDeleteI 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
I can`t download, url is break.
ReplyDeleteSource code download is not working, please reupload in mediafire
ReplyDeletePlease download from this link:
ReplyDeletehttps://skydrive.live.com/?sc=documents&cid=dbc6b335a854cc0e#cid=DBC6B335A854CC0E&id=DBC6B335A854CC0E%21113
Thank you
ReplyDeleteDownload the Latest Source Code
ReplyDeletehttp://code.msdn.microsoft.com/WPF-SAP-Crystal-report-e9c878b8
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.
ReplyDeleteYou 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.
ReplyDeletei have do what you have done completely same but i got an exception on reportutility class.
ReplyDeleteObject reference not set to an instance of an object.
please help
Hi Aimanzaki,
ReplyDeleteCheck the method "Display_report".
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