mikespiteri
mikespiteri

Reputation: 901

No valid report source is available- crystal reports

I have created a report using crystal reports. I am using visual studio 2010. The problem occurs when I try to go to another page. When I try to navigate to page 2 or last page the error No valid report source is available appears on the screen. Does anyone have any idea what I need to do? Thanks for your time

Upvotes: 5

Views: 11770

Answers (8)

Mohamed Nor
Mohamed Nor

Reputation: 633

#Simple Solution

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

yasir ali
yasir ali

Reputation: 3

try looking for some parameter that your report does not contain

usually it happens when you insert some parameter in code and your report does not contain that parameter

Upvotes: 0

Nick
Nick

Reputation: 3347

As stated in other answers kind of. Store the ReportDocument in a session (or something) and set the ReportSource.

Example:

protected void Page_Load(object sender, EventArgs e)
{
          if (Session["ReportSource"] != null)
          {
             CrystalReportViewer1.ReportSource = (ReportDocument) Session["ReportSource"];
          }
          else
          {
             ReportDocument reportDocument = new ReportDocument();
             reportDocument.Load("MyReport.rpt");
             CrystalReportViewer1.ReportSource = reportDocument;
             Session["ReportSource"] = reportDocument;
          }
}

Upvotes: 0

Muhammad Omer Khan
Muhammad Omer Khan

Reputation: 1

Well its not a big problem you just need to create session of your data source. Then pass it on page load. All crystal report function will work properly.

If you need any further help or code, let me know.

Upvotes: 0

aleafonso
aleafonso

Reputation: 2256

The following should solve your issue:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //whatever you do when the page is loaded for the first time
            //this could even be bindReport();
        }
        else
        {
            bindReport();
        }
    }

public void bindReport()
    {
        ReportDocument rptDoc = new ReportDocument();
        dsSample ds = new dsSample(); // .xsd file name
        DataTable dt = new DataTable();
        // Just set the name of data table
        dt.TableName = "Crystal Report Example";
        dt = getMostDialledNumbers(); //This function populates the DataTable
        ds.Tables[0].Merge(dt, true, MissingSchemaAction.Ignore);
        // Your .rpt file path will be below
        rptDoc.Load(Server.MapPath("mostDialledNumbers.rpt"));
        //set dataset to the report viewer.
        rptDoc.SetDataSource(ds);
        CrystalReportViewer1.ReportSource = rptDoc;
        CrystalReportViewer1.RefreshReport();
        //in case you have an UpdatePanel in your page, it needs to be updated
        UpdatePanel1.Update();
    }

Upvotes: 2

Sunil
Sunil

Reputation: 2905

Thanks to @Răzvan Panda.
Full code is given here.

protected void Page_Load(object sender, EventArgs e){ 
    CrystalDecisions.CrystalReports.Engine.ReportDocument report =
                            new CrystalDecisions.CrystalReports.Engine.ReportDocument();

                        TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
                        TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();

                        ConnectionInfo crConnectionInfo = new ConnectionInfo();
                        Tables CrTables;
                        report.Load(Server.MapPath("~/Reports/Monthly/CrMonthly.rpt"));

                        crConnectionInfo.ServerName = ConfigurationManager.AppSettings["Server4Crystal"].ToString();//[SQL SERVER NAME]
                        crConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["Database4Crystal"].ToString();//[DATABASE NAME]

                        crConnectionInfo.UserID = ConfigurationManager.AppSettings["User4Crystal"].ToString();//[DB USER NAME]
                        crConnectionInfo.Password = ConfigurationManager.AppSettings["Password4Crystal"].ToString(); //[DB PASSWORD]

    //LOOP THROUGH EACH TABLE & PROVIDE LOGIN CREDENTIALS
                        CrTables = report.Database.Tables;
                        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
                        {
                            crtableLogoninfo = CrTable.LogOnInfo;
                            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                            CrTable.ApplyLogOnInfo(crtableLogoninfo);
                        }

    //PROVIDE PARAMETERS TO CRYSTAL REPORT, IF REQUIRED.
                        ParameterField paramField = new ParameterField();
                        ParameterFields paramFields = new ParameterFields();
                        ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

                        paramField.Name = "PARAMETER_NAME";
                        paramDiscreteValue.Value = "PARAMETER_VALUE";
                        paramField.CurrentValues.Add(paramDiscreteValue);
                        paramFields.Add(paramField);
    //ADD MORE PARAMETERS, IF REQUIRED....


                        StoreMonthlyViewer.ParameterFieldInfo = paramFields;
                        StoreMonthlyViewer.ReportSource = report;
    //FINALLY REFRESH REPORT
                        StoreMonthlyViewer.RefreshReport();
                        StoreMonthlyViewer.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
}

Upvotes: 0

Khalid Rafique
Khalid Rafique

Reputation: 161

Store you report in Session and then give report source from session on page post back

protected void Page_Load(object sender, EventArgs e)
{
       if (IsPostBack)
        {
            try
            {
                CrystalReportViewer1.ReportSource = (ReportDocument)Session["Report"];
                CrystalReportViewer1.RefreshReport();
                CrystalReportViewer1.DataBind();
            }
            catch (Exception ex)
            {

               // throw;
            } 
        }

    }
    protected void CrystalReportViewer1_PreRender(object sender, EventArgs e)
    {

    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        ReportDocument rptDoc = new ReportDocument();
        rptDoc.Load(Server.MapPath("Reports\\BalanceReportNew\\BalanceReport.rpt"));
        rptDoc.SetDataSource(ReportData());
        Session["Report"] = rptDoc;
        CrystalReportViewer1.ReportSource = rptDoc;
        CrystalReportViewer1.RefreshReport();
        CrystalReportViewer1.DataBind();
    }
    public DataTable ReportData()
    {
        string ClassName = ddlClass.SelectedValue;
        string Division = ddlDivison.SelectedValue;
        string Subject = ddlSubjects.SelectedValue;
        DataTable ReportData = objRpt.getReportData(ClassName, Division, Subject);
        return ReportData;
    }

Upvotes: 2

Răzvan Flavius Panda
Răzvan Flavius Panda

Reputation: 22116

Try using the solution in this thread:

No Valid report source is available

From what it says, you should be able to make it work by providing ConnectionInfo and ReportSource in the code.

Upvotes: 1

Related Questions