不使用报表查看器RDLC子报表加载错误

时间:2014-02-13 08:29:43

标签: c# rdlc

我有RDLC报告,有报告。当我使用报表查看器时,系统工作正常。但是当我想加载没有报表查看器的报表时,系统会返回错误。请帮助我。感谢

    public void DetailsReport()
            {
                string mimeType;
                byte[] renderedBytes;

                string reportPath = "rptAllEmployees.rdlc";//report path
                string datasetName = "my";//report datasource
                decimal reportWidth = 11.5m;
                decimal reportHeight = 8.5m;
                PopulateReport( CustomDS.GetAllEmployees(), datasetName, reportPath, out mimeType, out renderedBytes, reportWidth, reportHeight);
                //return File(renderedBytes, mimeType);

            }
            private void PopulateReport(List<Employee> objectList, string datasetName, string reportPath, out string mimeType, out byte[] renderedBytes, decimal fileWidth, decimal fileHeight)
            {
                LocalReport localReport = new LocalReport();
                localReport.ReportPath = Server.MapPath(reportPath);
                ReportDataSource reportDataSource = new ReportDataSource(datasetName, objectList);
                localReport.SubreportProcessing +=
                    new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
                localReport.DataSources.Add(reportDataSource);
                //localReport.SetParameters(new ReportParameter("pm", "", false));
                string reportType = "PDF";
                mimeType = string.Empty;
                string encoding = string.Empty;
                string fileNameExtension = string.Empty;
                //The DeviceInfo settings should be changed based on the reportType
                string deviceInfo =

                "<DeviceInfo>" +

                "  <OutputFormat>PDF</OutputFormat>" +

                "  <PageWidth>" + fileWidth + "in</PageWidth>" +

                "  <PageHeight>" + fileHeight + "in</PageHeight>" +

                "  <MarginTop>0.5in</MarginTop>" +

                "  <MarginLeft>1in</MarginLeft>" +

                "  <MarginRight>1in</MarginRight>" +

                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
                Warning[] warnings;
                string[] streams;
                //Render the report
                 //Eror occured in this point
                **renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);**
                //Clear the response stream and write the bytes to the outputstream
                //Set content-disposition to "attachment" so that user is prompted to take an action
                //on the file (open or save)
                Response.Clear();
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
                Response.BinaryWrite(renderedBytes);
                Response.End();
            }
            void LocalReport_SubreportProcessing(
                object sender, 
                Microsoft.Reporting.WebForms.SubreportProcessingEventArgs e)
            {
                // get empID from the parameters
                int iEmpID = Convert.ToInt32(e.Parameters[0].Values[0]);

                // remove all previously attached Datasources, since we want to attach a
                // new one
                e.DataSources.Clear();

                // Retrieve employeeFamily list based on EmpID
                var employeeFamily = CpReportCustomData.Data.CustomDS.GetAllEmployeeFamily()
                                     .FindAll(element => element.ID == iEmpID);

                // add retrieved dataset or you can call it list to data source
                e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource()
                {
                    Name = "DSEmployeeFamily",
                    Value = employeeFamily

                });

            }

 public class CustomDS
    {
        private static List<Employee> _lstEmployee = null;
        public static List<Employee> GetAllEmployees()
        {
            if (_lstEmployee == null)
            {
                _lstEmployee = new List<Employee>();
                _lstEmployee.Add( new Employee()
                {
                    ID=1,
                    Name="Alok",
                    Age=30
                });

                _lstEmployee.Add(new Employee()
                {
                    ID = 2,
                    Name = "Ashish",
                    Age = 30
                });

                _lstEmployee.Add(new Employee()
                {
                    ID = 3,
                    Name = "Jasdeep",
                    Age = 30
                });

                _lstEmployee.Add(new Employee()
                {
                    ID = 4,
                    Name = "Kamlesh",
                    Age = 31
                });

            }



            return _lstEmployee;
        }


        private static List<EmployeeFamily> _lstEmployeeFamily = null;

        public static List<EmployeeFamily> GetAllEmployeeFamily()
        {
            if (_lstEmployeeFamily == null)
            {
                _lstEmployeeFamily = new List<EmployeeFamily>();
                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 1,
                    Name = "AlokWife",
                    Relation = "Wife"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 1,
                    Name = "AlokDaughter",
                    Relation = "Daughter"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 2,
                    Name = "AshishWife",
                    Relation = "Wife"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 3,
                    Name = "JasdeepFather",
                    Relation = "Father"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 3,
                    Name = "JasdeepMother",
                    Relation = "Mother"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 4,
                    Name = "KamleshWife",
                    Relation = "Wife"

                });

                _lstEmployeeFamily.Add(new EmployeeFamily()
                {
                    ID = 4,
                    Name = "KamleshDaughter",
                    Relation = "Daughter"

                });
            }

            return _lstEmployeeFamily;
        }
public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
 public class EmployeeFamily
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public string Relation { get; set; }
    }

0 个答案:

没有答案