简体   繁体   中英

displaying a crystal report using c#

I am relatively new to C# and i have never used Crystal Reports so i apologise if i do use the incorrect terminology. I am trying to display a report which is called by some C# code. By following a lot of threads on here, i have managed to come up with the following code which does build an debug. However, when the code is run, it does not display the report.

Here is the code:

private void forAllQualitiesToolStripMenuItem_Click(object sender, EventArgs e) {
    CrystalReportViewer rv = new CrystalReportViewer();
    string reportPath = @"C:\Documents and Settings\rp\Desktop\StockByStatus.rpt"; 

    ReportDocument r = new ReportDocument();

    r.Load(reportPath);
    rv.Visible = false; // i put this in because when i ran the code without it, it said the report must not be visible and the program would fall down
    rv.ReportSource = r;
    rv.InitReportViewer();
    ShowDialog(rv);
}
    ReportDocument cryRpt = new ReportDocument();
    cryRpt.Load(@"CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");
    crystalReportViewer1.ReportSource = cryRpt;
    crystalReportViewer1.Refresh();
private void forAllQualitiesToolStripMenuItem_Click(object sender, EventArgs e) {

                    {
                        CrystalReportViewer rv = new CrystalReportViewer();
                        doc = new ReportDocument();
                        doc.Load(Server.MapPath("MR.rpt"));
                        doc.SetDatabaseLogon("sa", "Admin123", "vivek", "PURCHASE", false);
                        reportdocument.SetParameterValue("@MRNO", ddlmrno.SelectedValue);
                        rv .ReportSource = doc;
                    }

Now Try this code as reference code....

Its Better you add windows form and drag and drop Crystel report viwer to form.It's automatically set as full screen.thart report viwer you can used view all crystel reports in your Application. Note:You need to install Crystel report run time compatible version according our Visual studio version.

在此处输入图片说明

Now you can call to report like this in Button press event

 private void btOPdetailRep_Click(object sender, EventArgs e)
 {
   try
  {
    load();
    frmReports.printproparty = 7;   //7 what i assign numer for identify report
    frmReports objshow = new frmReports();
    objshow.ShowDialog();
  }
    catch (Exception ex)
  {
    MessageBox.Show("Details Printing Error!");
  }
}

then in report form loading event write this code

string username = "sa";       //USERNAME AND PASSWORD FOR REPORT LOADING
 string password = "123";
if (printproparty == 7)
  {
   ReportDocument cryRpt = new ReportDocument();
   cryRpt.Load(@"op payment.rpt");
   cryRpt.SetDatabaseLogon(username, password);
   reports.ReportSource = cryRpt;
   reports.RefreshReport();
   reports.Refresh();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM