无法为数据集创建数据读取器' barcode1' :ReportingProcessException

发布于 2025-01-21 21:09:07 字数 5909 浏览 0 评论 0原文

我正在创建一个Web表单应用程序,其中用户单击“打印”按钮并立即以形式连接到btnonclick方法的rdlc文件,但它给了我这个错误

reportprocessingexception:无法为dataSet'barcode1'创建数据读取器,

直到我正常工作,直到我工作得很好地工作了将数据集添加到我的rdlc中以打印出barcodes

forms1

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintToPrinter
 {
public partial class Form1 : Form
{
    object sender;
    EventArgs e;
    public Form1()
    {
        InitializeComponent();
        barcodeTxt.Text = "3kg";//change text here
        if (barcodeTxt.Text != null)
        {
            button1_Click(sender,e);
        }

        
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = Application.StartupPath + "\\SetItemReport.rdlc";
        localReport.PrintToPrinter();
        DataSet ds = new DataSet();
        ReportDataSource barcodeData = new ReportDataSource();
        barcodeData.Name = "Barcode1";
        barcodeData.Value = ds.Tables["BarcodeData"];
        localReport.DataSources.Add(barcodeData);
        localReport.Refresh();

        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        BarcodeLib.Barcode barcode = new BarcodeLib.Barcode();
        Image img = barcode.Encode(BarcodeLib.TYPE.CODE128, barcodeTxt.Text, Color.Black, Color.White, 100, 30);
        pictureBox1.Image = img;
        this.barcodeData1.Clear();
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, ImageFormat.Png);
            this.barcodeData1.Barcode1.AddBarcode1Row(barcodeTxt.Text, ms.ToArray());
        }
        using (frmReport frm = new frmReport(this.barcodeData1.Barcode1))
        {
            frm.ShowDialog();
        }
        
    }
}

}

localreportextentsion(printToproPoproNterClass)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace PrintToPrinter
{
public static class LocalReportExtensionscs
{
    public static void PrintToPrinter(this LocalReport report)
    {
        var pageSettings = new PageSettings();
        pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
        pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
        pageSettings.Margins = report.GetDefaultPageSettings().Margins;
        pageSettings.Landscape = true;
        Print(report, pageSettings);
    }

    public static void Print(this LocalReport report, PageSettings pageSettings)
    {
        string deviceInfo =
            $@"<DeviceInfo>
            <OutputFormat>EMF</OutputFormat>
            <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
            <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
            <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
            <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
            <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
            <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
        </DeviceInfo>";

        Warning[] warnings;
        var streams = new List<Stream>();
        var currentPageIndex = 0;
       Error is thrown here --> 
        **report.Render("Image", deviceInfo,   
            (name, fileNameExtension, encoding, mimeType, willSeek) =>
            {
                var stream = new MemoryStream();
                streams.Add(stream);
                return stream;
            }, out warnings);**

        foreach (Stream stream in streams)
            stream.Position = 0;

        if (streams == null || streams.Count == 0)
            throw new Exception("Error: no stream to print.");

        var printDocument = new PrintDocument();
        printDocument.DefaultPageSettings = pageSettings;
        if (!printDocument.PrinterSettings.IsValid)
            throw new Exception("Error: cannot find the default printer.");
        else
        {
            printDocument.PrintPage += (sender, e) =>
            {
                Metafile pageImage = new Metafile(streams[currentPageIndex]);
                Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);
                e.Graphics.DrawImage(pageImage, adjustedRect);
                currentPageIndex++;
                e.HasMorePages = (currentPageIndex < streams.Count);
                e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
            };
            printDocument.EndPrint += (Sender, e) =>
            {
                if (streams != null)
                {
                    foreach (Stream stream in streams)
                        stream.Close();
                    streams = null;
                }
            };
            printDocument.Print();
        }
    }
}

}

错误

Error LocalReportExtensionscs.cs:line 43
Erro  LocalReportExtensionscs.cs:line 24
Form1.cs:line 36
at PrintToPrinter.Program.Main() in Program.cs:line 19

 This exception was originally thrown at this call stack:
[External Code]

Im creating a web form app where the user clicks the print button and instantly prints the RDLC file connected to the BtnOnClick method in form but it gives me this error

ReportProcessingException: Unable to create data reader for dataset'Barcode1'

It worked perfectly fine until i added datasets to my rdlc to print out barcodes

forms1

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintToPrinter
 {
public partial class Form1 : Form
{
    object sender;
    EventArgs e;
    public Form1()
    {
        InitializeComponent();
        barcodeTxt.Text = "3kg";//change text here
        if (barcodeTxt.Text != null)
        {
            button1_Click(sender,e);
        }

        
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = Application.StartupPath + "\\SetItemReport.rdlc";
        localReport.PrintToPrinter();
        DataSet ds = new DataSet();
        ReportDataSource barcodeData = new ReportDataSource();
        barcodeData.Name = "Barcode1";
        barcodeData.Value = ds.Tables["BarcodeData"];
        localReport.DataSources.Add(barcodeData);
        localReport.Refresh();

        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        BarcodeLib.Barcode barcode = new BarcodeLib.Barcode();
        Image img = barcode.Encode(BarcodeLib.TYPE.CODE128, barcodeTxt.Text, Color.Black, Color.White, 100, 30);
        pictureBox1.Image = img;
        this.barcodeData1.Clear();
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, ImageFormat.Png);
            this.barcodeData1.Barcode1.AddBarcode1Row(barcodeTxt.Text, ms.ToArray());
        }
        using (frmReport frm = new frmReport(this.barcodeData1.Barcode1))
        {
            frm.ShowDialog();
        }
        
    }
}

}

LocalReportExtentsion(PrintToPrinterClass)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace PrintToPrinter
{
public static class LocalReportExtensionscs
{
    public static void PrintToPrinter(this LocalReport report)
    {
        var pageSettings = new PageSettings();
        pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
        pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
        pageSettings.Margins = report.GetDefaultPageSettings().Margins;
        pageSettings.Landscape = true;
        Print(report, pageSettings);
    }

    public static void Print(this LocalReport report, PageSettings pageSettings)
    {
        string deviceInfo =
            $@"<DeviceInfo>
            <OutputFormat>EMF</OutputFormat>
            <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
            <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
            <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
            <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
            <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
            <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
        </DeviceInfo>";

        Warning[] warnings;
        var streams = new List<Stream>();
        var currentPageIndex = 0;
       Error is thrown here --> 
        **report.Render("Image", deviceInfo,   
            (name, fileNameExtension, encoding, mimeType, willSeek) =>
            {
                var stream = new MemoryStream();
                streams.Add(stream);
                return stream;
            }, out warnings);**

        foreach (Stream stream in streams)
            stream.Position = 0;

        if (streams == null || streams.Count == 0)
            throw new Exception("Error: no stream to print.");

        var printDocument = new PrintDocument();
        printDocument.DefaultPageSettings = pageSettings;
        if (!printDocument.PrinterSettings.IsValid)
            throw new Exception("Error: cannot find the default printer.");
        else
        {
            printDocument.PrintPage += (sender, e) =>
            {
                Metafile pageImage = new Metafile(streams[currentPageIndex]);
                Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);
                e.Graphics.DrawImage(pageImage, adjustedRect);
                currentPageIndex++;
                e.HasMorePages = (currentPageIndex < streams.Count);
                e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
            };
            printDocument.EndPrint += (Sender, e) =>
            {
                if (streams != null)
                {
                    foreach (Stream stream in streams)
                        stream.Close();
                    streams = null;
                }
            };
            printDocument.Print();
        }
    }
}

}

The error

Error LocalReportExtensionscs.cs:line 43
Erro  LocalReportExtensionscs.cs:line 24
Form1.cs:line 36
at PrintToPrinter.Program.Main() in Program.cs:line 19

 This exception was originally thrown at this call stack:
[External Code]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文