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

Danny Melander 1 Reputation point
2022-04-18T00:43:44.747+00:00

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]
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,885 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.