Search code examples
c#asp.netcrystal-reports-2010

Dynamic query for Crystal Report, "No valid report source is available."


I am trying to create a Crystal Report with a dynamic data source. When i click the report button, the crystal report shows up just fine, but when i click any button the Crystal Report tool bar (i.e. Next page, Export To, etc.), i get a pop with the Error "No valid report source is available."

Here is what my C# code looks like:

private void GenerateReport()
{
    using (DataContext reports = new DataContext())
    {
        var results = //Linq Query;

        ReportDocument pr = new ReportDocument();
        pr.Load(Server.MapPath(@"CrystalReport1.rpt"));
        pr.SetDataSource(results.ToList());

        CrystalReportViewer1.ReportSource = pr;
        CrystalReportViewer1.DataBind();
    }
}

Any help would be greatly appreciated. Thanks in advanced.


Solution

  • So, I could not get this working with the other way, but i did get it to work going about it a different way. First, i added a CrystalReportSource control to the aspx page,

    <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
       <Report FileName="CrystalReport1.rpt">
      </Report>
    </CR:CrystalReportSource>
    

    Then in the code behind file, instead of setting the datasource directly to CrystalReportViewer1, i set it to CrystalReportSource, then i set that as the report source for CrystalReportViewer1. Now everything works just fine!

    using (DataContext reports = new DataContext())
    {
        var results = //Linq Query;
        CrystalReportSource1.ReportDocument.SetDataSource(results.AsEnumerable());
    
        CrystalReportViewer1.ReportSourceID = "CrystalReportSource1";
        CrystalReportViewer1.RefreshReport();
    }