Reputation: 486
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.
Upvotes: 1
Views: 4723
Reputation: 623
I have just Solved this problem using CrystalReportViewer Navigate Event
in View report Button i have saved report document in a session
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
' -- the ds is dataset variable containing data to be displayed in the report
rptDoc.SetDataSource(ds)
Session.Add("rptdoc", rptDoc)
CrystalReportViewer1.ReportSource = rptDoc
End Sub
then in Navigate event of CrystalReportViewer i set the CrystalReportViewer data source to the Session
Protected Sub j(ByVal source As Object, ByVal e As CrystalDecisions.Web.NavigateEventArgs) Handles CrystalReportViewer1.Navigate
rpt.SetDataSource(ds)
CrystalReportViewer1.ReportSource = session("rptdoc")
End Sub
So each time before you navigate to another page in the report , CrystalReportViewer data source is set to the report document saved in the session.
Upvotes: 1
Reputation: 486
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();
}
Upvotes: 1