报告查看器导出到pdf获取错误

时间:2014-09-10 13:18:23

标签: c# pdf rdlc

我收到此错误"无法为数据集创建数据读取器' DataSet1'。"

我花了很多时间来解决这个问题但却无法解决 已解决.Same代码适用于报告生成,但在生成pdf时它会停滞不前。

这是我的代码,请回复。

 protected void btnPdf_Click(object sender, EventArgs e)
        {
            string PDF = "PDF";
            string ReportType = "ReportType";
            Warning[] warnings = null;
            string[] streamIds = null;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            string filetype = string.Empty;

            long _landIds = 0;
            if (_farmId > 0)
            {
                Land land = LandManager.GetLandByFarmID(_farmId);
                _landIds = land.LandID;
            }


            ReportViewer_MyReportID.SizeToReportContent = true;
            ReportViewer_MyReportID.LocalReport.ReportPath = "reports/report/report.rdlc";
            ReportViewer_MyReportID.ProcessingMode = ProcessingMode.Remote;

            ObjectDataSource_Id.SelectParameters.Clear();
            ObjectDataSource_Id.SelectParameters.Add(QueryStringEnum.CompanyID, CurrentCompanyID.ToString());
            ObjectDataSource_Id.SelectParameters.Add(QueryStringEnum.LandID, _landIds.ToString());
            var days = "-" + rdDuration.SelectedValue;
            ObjectDataSource_Id.SelectParameters.Add(QueryStringEnum.Days, days.ToString());

            ReportViewer_MyReportID.LocalReport.Refresh();

            byte[] bytes = ReportViewer_MyReportID.LocalReport.Render("PDF", null,
             out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fs = new FileStream(Server.MapPath("~/GeneratedFiles/" + ReportType + "." + "PDF"), FileMode.OpenOrCreate);

            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

        }

1 个答案:

答案 0 :(得分:2)

它适合您。确保数据集名称没有区别。

代码供您参考。

protected void btnPdf_Click(object sender, EventArgs e)
    {
        ReportViewer viwer = new ReportViewer();
        ObjectDataSource ob = new ObjectDataSource("dataset.YourTableAdapter", "GetData");
        dataset.YourTableAdapter ds = new dataset.YourTableAdapter();

        string PDF = "PDF";
        string ReportType = "ReportType";
        Warning[] warnings = null;
        string[] streamIds = null;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        string filetype = string.Empty;

        long _landIds = 0;
        if (_farmId > 0)
        {
            Land land = LandManager.GetLandByFarmID(_farmId);
            _landIds = land.LandID;
        }

        viwer.SizeToReportContent = true;
        viwer.LocalReport.ReportPath = "reports/report/report.rdlc";
        viwer.ProcessingMode = ProcessingMode.Local;
        ob.SelectParameters.Clear();
        ob.SelectParameters.Add(QueryStringEnum.CompanyID, CurrentCompanyID.ToString());
        ob.SelectParameters.Add(QueryStringEnum.LandID, _landIds.ToString());
        var days = "-" + rdDuration.SelectedValue;
        ob.SelectParameters.Add(QueryStringEnum.Days, days.ToString());

        ReportDataSource rds = new ReportDataSource("datasetname", (object) ds.GetData((long?)CurrentCompanyID.ToInt64(), (int?)days.ToInt(), (long?)_landIds.ToInt64()));

        viwer.LocalReport.DataSources.Add(rds);
        viwer.LocalReport.Refresh();

        byte[] bytes = viwer.LocalReport.Render("PDF", null,
         out mimeType, out encoding, out extension, out streamIds, out warnings);
        FileStream fs = new FileStream(Server.MapPath("~/GeneratedFiles/" + ReportType + "." + "PDF"), FileMode.OpenOrCreate);

        fs.Write(bytes, 0, bytes.Length);
        fs.Close();



    }