Search code examples
c#asp.net-mvccrystal-reports

The System cannot find the path specified for some crystal reports


I am using Crystal Reports latest edition in MVC application. 1 report is working fine but a very weird error is occurring continuously in loading the other reports. I have used Switch statement. The report in first case is running fine. But in other cases there is always an error:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in myapp.dll but was not handled in user code Additional Information: The system cannot find the path specified.

public ActionResult SeeReport(FormCollection f)
{
    ReportDocument rd = new ReportDocument();

    string s = f["rptname"];
    switch (s)
    {
        case ("011501"):
        {
            DataSet ds = new DataSet();

            CommonProcs_AHPI.CommonProcess xRptProcess = new CommonProcs_AHPI.CommonProcess();
            ds.ExtendedProperties["index"] = 1150101;//13502 013501_3
            DataTable xDTR = new DataTable();
            xDTR.TableName = "_rpTitle_";

            xDTR.Columns.Add("N100", System.Type.GetType("System.Double"));
            xDTR.Columns.Add("S100", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S101", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S102", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S1", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S2", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S3", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S4", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S5", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S6", System.Type.GetType("System.String"));
            ds.Tables.Add(xDTR);

            DataTable xDT = new DataTable();
            xDT.TableName = "rpdata";
            xDT.Columns.Add("S102", System.Type.GetType("System.String"));
            xDT.Columns.Add("S107", System.Type.GetType("System.String"));
            xDT.Columns.Add("S20", System.Type.GetType("System.String"));
            xDT.Columns.Add("S21", System.Type.GetType("System.String"));
            xDT.Columns.Add("S22", System.Type.GetType("System.String"));
            xDT.Columns.Add("S23", System.Type.GetType("System.String"));
            xDT.Columns.Add("S24", System.Type.GetType("System.String"));
            xDT.Columns.Add("S25", System.Type.GetType("System.String"));

            DataRow xRow = xDT.NewRow();

            xRow["S102"] = "1";
            xRow["S21"] = "4";
            xRow["S22"] = f["begin_code"];
            xRow["S23"] = f["end_code"];
            xRow["S24"] = "N";
            xRow["S25"] = "";
            xDT.Rows.Add(xRow);
            ds.Tables.Add(xDT);
            xDT.AcceptChanges();

            ds = xRptProcess.ProcessCall(ds);

            rd.Load(Path.Combine(Server.MapPath("~/RPTGL_AHPI/"), "011501.rpt"));
            rd.SetDataSource(ds);
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            break;
        }
        case ("013501_3"):
        {
            DataSet ds = new DataSet();

            CommonProcs_AHPI.CommonProcess xRptProcess = new CommonProcs_AHPI.CommonProcess();
            ds.ExtendedProperties["index"] = 13502;
            DataTable xDTR = new DataTable();
            xDTR.TableName = "_rpTitle_";

            xDTR.Columns.Add("N100", System.Type.GetType("System.Double"));
            xDTR.Columns.Add("S100", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S101", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S102", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S1", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S2", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S3", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S4", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S5", System.Type.GetType("System.String"));
            xDTR.Columns.Add("S6", System.Type.GetType("System.String"));
            ds.Tables.Add(xDTR);

            DataTable xDT = new DataTable();
            xDT.TableName = "rpdata";
            xDT.Columns.Add("S102", System.Type.GetType("System.String"));
            xDT.Columns.Add("S107", System.Type.GetType("System.String"));
            xDT.Columns.Add("S20", System.Type.GetType("System.String"));
            xDT.Columns.Add("S21", System.Type.GetType("System.String"));
            xDT.Columns.Add("S22", System.Type.GetType("System.String"));
            xDT.Columns.Add("S23", System.Type.GetType("System.String"));
            xDT.Columns.Add("S24", System.Type.GetType("System.String"));
            xDT.Columns.Add("S25", System.Type.GetType("System.String"));
            xDT.Columns.Add("D1", System.Type.GetType("System.DateTime"));
            xDT.Columns.Add("D2", System.Type.GetType("System.DateTime"));

            DataRow xRow = xDT.NewRow();

            xRow["S102"] = "1";
            //xRow["S21"] = "4";
            xRow["S21"] = f["begin_code"];
            xRow["S22"] = f["end_code"];
            xRow["S23"] = "1";
            xRow["S24"] = "0";
            xRow["S25"] = "";
            xRow["D1"]  = Convert.ToDateTime(f["begin_date"]);
            xRow["D2"]  = Convert.ToDateTime(f["end_date"]);
            xDT.Rows.Add(xRow);
            ds.Tables.Add(xDT);
            xDT.AcceptChanges();

            ds = xRptProcess.ProcessCall(ds);

            rd.Load(Path.Combine(Server.MapPath("~/Report/"), "013501_3.rpt"));
            rd.SetDataSource(ds);
            rd.Refresh();
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            break;

        }


    }

    try
    {
        Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);
        return File(stream, "application/pdf", "Report.pdf");
    }
    catch (Exception ex)
    {
        throw;
    }
}

The first case works fine and the reports is successfully downloaded but all other reports generate the error mentioned above.


Solution

  • I got it... My report was erroneous. The error "System Cannot find the path specified" was very much misleading. Actually, when the report goes for STREAM it looks for the report data but since it is corrupt so it didn't get the specified report and throws the exception of path not found. I solved it by making a WPF application inside my app and viewed the report from there and then I got the real error about my report.