jeudi 2 avril 2015

How to show a fully configured crystal reprot in C# without using a dataset?

I'v designed a crystal reprot using crystal report 11.0.0.895.


I configured the connection to a database and everything is working just fine from inside the crystal report application.


Now I want to show the reprot when a button is clicked in a windows form C# application. I use this code:



DataTable dt = supervisorBLC.M_rpt_task2();
DataSet ds = new DataSet();
if (dt.Rows.Count > 0)
{
ds.Tables.Add(dt);
}

ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(@"C:\Users\msabry\Desktop\Report1.rpt");
crystalReportViewer1.ReportSource = reportDocument;
reportDocument.SetDatabaseLogon("sa", "P@ssw0rd", "(local)", "FirstTask");
reportDocument.SetDataSource(ds);

crystalReportViewer1.ReportSource = reportDocument;
crystalReportViewer1.Refresh();
crystalReportViewer1.Dock = DockStyle.Fill;
this.Controls.Add(crystalReportViewer1);


It works just fine.


My question is why must I use a dataset and populate it with data from the database while the report itself gets everything it needs from the database directily? Can't I just give the ReportDocument object the path to the already created report and give this document to the viewer?


Aucun commentaire:

Enregistrer un commentaire