Question

I've created a crystal report then after creating it, I've created a winform where I've import crystal report library (shown in code) and used a report viewer to view the report but i not able to view the report, the code, i am new with Crytal Reports, the code I've done is :

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace InventorySoftware
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string ReportSources = "";
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load("C:\\Users\\Ahsan\\Desktop\\PROJECT INVENTORY SOFTWARE\\InventorySoftware\\InventorySoftware\\CrystalReport1.rpt");
            reportViewer1.ReportSource = cryRpt;
            reportViewer1.Refresh();

        }
    }
}

it's giving error at reportViewer1.ReportSource = cryRpt; and the error is

Error   1   'Microsoft.Reporting.WinForms.ReportViewer' does not contain a definition for 'ReportSource' and no extension method 'ReportSource' accepting a first argument of type 'Microsoft.Reporting.WinForms.ReportViewer' could be found (are you missing a using directive or an assembly reference?) C:\Users\Ahsan\Desktop\PROJECT INVENTORY SOFTWARE\InventorySoftware\InventorySoftware\Form1.cs  34  27  InventorySoftware
Was it helpful?

Solution

You're using the wrong classes/controls for Crystal Reports.

Place a CrystalReportViewer control on your form. Although with later versions of Visual Studio you have to download it separately, it was still shipped with VS2008.

If you don't see it in your toolbox, right-click anywhere in your toolbox and click "Choose Items...".

enter image description here

After checking it and pressing OK, it should be added to your toolbox. Remove your existing report control and drop a crystal report viewer on the form:

enter image description here

The necessary crystal references will be added to your project when you drop the viewer on it.

Add this using directive to the top of your code-behind:

using CrystalDecisions.CrystalReports.Engine;

Then load your report into the viewer:

var cryRpt = new ReportDocument();
cryRpt.Load(@"C:\Users\Ahsan\Desktop\PROJECT INVENTORY SOFTWARE\InventorySoftware\InventorySoftware\CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();

EDIT:

Change the targeted framework from .NET Framework 4 Client Profile to .NET Framework 4:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top