question

DannyMelander-4248 avatar image
0 Votes"
DannyMelander-4248 asked JackJJun-MSFT commented

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

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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

People in the other forums are saying it might possibly be because my Dataset

  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();
            
  }

Doesnt have the BarcodeData.xsd class in it? Thoughts?

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT DannyMelander-4248 ·

@DannyMelander-4248, Welcome to Microsoft Q&A,

 People in the other forums are saying it might possibly be because my Dataset

I also agree with the idea, based on your code, I only see the ds is created, but I didn't see how the table BarcodeData is created. I suggest that you could set a break point to check if the dataset is null.

0 Votes 0 ·

0 Answers