Home » c# » c# – CrystalDecisions.Shared.CrystalReportsException:Load report failed. The document has not opened-Exceptionshub

c# – CrystalDecisions.Shared.CrystalReportsException:Load report failed. The document has not opened-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have method and I am using crystal-decision report and its throwing an inner exception. How do I fix this issue? I have seen similar topic, but mine does not really solve similar problem as I have attempted to do the same. Please see my logic below and screen shot for more detail. In another words my load method does not open.

// Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AdvertReport(FormCollection fc)
{
    DataSet ds = obIlReports.Generate_AdvertDetailsReport();
    ds.Tables[0].TableName = "Tbl_TrainingAcademy";

    if(ds.Tables[0].Rows.Count > 0)
    {
        ReportClass rptH = new ReportClass();
        rptH.FileName = Server.MapPath("~/Reports/AdvertReport.rpt");
        rptH.Load(); // The document do not open error is thrown here.
        rptH.SetDataSource(ds.Tables[0]);
        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();


        Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);
        return File(stream, "application/pdf", "AdvertReport.pdf");
    }
    return View();
}
// Ilreport here with store procedure.
public class ReportsMaster : IlReports
{
    public DataSet Generate_AdvertDetailsReport()
    {
        using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["eNtsaOnlineRegistrationDB"].ToString()))
        {
            con.Open();
            DataSet ds = new DataSet();

            // Handling Exception
            try
            {
                SqlCommand cmd = new SqlCommand("dbo.GetAdvertReport", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);

                if(ds.Tables.Count > 0)
                {
                    return ds;
                }
                else
                {
                    return ds = null;
                }

            }
            catch(Exception )
            {
                throw;
            }
            finally
            {
                ds.Dispose();
            }
        }
    }
}
// ConnectionString
<connectionStrings>
    <add name="eNtsaOnlineRegistrationDB" connectionString="Data Source=GcobaniM-L\SQLEXPRESS; DataBase=eNtsaOnlineRegistrationDB; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
How to&Answers:
  1. The problem i had was simple, i created report file without having an extension report like crystal report install for Microsoft Visual Studio. Here is the link for Windows for your compatibility.

https://www.sap.com/cmp/td/sap-crystal-reports-visual-studio-trial.html?trial=%2F%2Fwww.sap.com%2Fregistration%2Ftrial.9a4afb3b-7eaa-42af-98ce-abeae5deb784.html

  1. After the installation of this software, the Visual Studio will have Reporting. See my screen shot for idea or clue.
    enter image description here

  2. Make sure your VS is closed until the software is done.

  3. Then you create your report and give a its path based on your project application.

  4. If you get an exception like one below;

Could not load file or assembly CrystalDecisions.CrystalReports.Engine

  1. Follow the steps as mentioned from the forum and your application should work, if not troubleshoot.