-1

I am printing reports using Report Viewer. I followed a tutorial about this code but I got this error "Cannot create a data reader for dataset 'Dataset1'.

I am using reportviewer and rdlc in this. I dont know if the problem is the the Reportdatasource itself or the dataset:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using MySql.Data.MySqlClient;

namespace Water_Quality_Monitoring
{
    public partial class FishpondForm : Form
    {

        Microsoft.Reporting.WinForms.ReportDataSource rs = new 
Microsoft.Reporting.WinForms.ReportDataSource();
        private DataGridView grid;

        public FishpondForm()
        {
            InitializeComponent();
        }

private void btnPrint_Click(object sender, EventArgs e)
        {
            List<Print> list = new List<Print>();
            list.Clear();

            for (int i = 0; i < agriInfo.dataAgriInfo.Rows.Count - 1; i++)
            {
                list.Add(new Print
                {
                  MemberID = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  LastName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  FirstName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  MiddleName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  Contact = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  Status = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString()
                });
                Microsoft.Reporting.WinForms.ReportDataSource rs = new 
                Microsoft.Reporting.WinForms.ReportDataSource();
                rs.Name = "Dataset1";
                rs.Value = list;
                PrintFishpond frm = new PrintFishpond();
                frm.reportViewer1.LocalReport.DataSources.Clear();
                frm.reportViewer1.LocalReport.DataSources.Add(rs);
                frm.reportViewer1.LocalReport.ReportEmbeddedResource = 
                "Water_Quality_Monitoring.Report1.rdlc";
                frm.ShowDialog();
            }
        }
    }

    public class Print
    {
        public string MemberID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string Contact { get; set; }
        public string Status { get; set; }

    }
}

I expect to print what is in the datagrid dataAgriInfo. I hope you can help me.

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
Narts
  • 11
  • 3
  • The error is. "Cannot create a data reader for dataset 'Dataset1' – Narts Sep 11 '19 at 03:17
  • It looks like the ReportDataSource.Value property is expecting a DataTable, not a list. – beeker Sep 11 '19 at 03:28
  • Possible duplicate of [.rdlc Report - Cannot create a data reader for dataset 'DataSet1'](https://stackoverflow.com/questions/15365731/rdlc-report-cannot-create-a-data-reader-for-dataset-dataset1) – Circle Hsiao Sep 12 '19 at 03:55

2 Answers2

0

Try putting the list into a datatable and assigning the datatable to the value property of the ReportDataSource. Use the function cut+paste from this post: How to convert a list into data table

Then...

rs.Value = table;
beeker
  • 780
  • 4
  • 17
  • cant access the ObjectReader – Narts Sep 11 '19 at 03:36
  • I don't have VS in front of me on this computer so can't test but, I posted a link to a similar SO post that has a function you can cut+paste and call to convert your List to a datatable. – beeker Sep 11 '19 at 03:41
0

You have to create a data table out of the datagrid and then assign it to the report data source as given below:

DataTable dt = new DataTable();
dt = ((DataView)dataAgriInfo.ItemsSource).ToTable(); 
dt.TableName = "TableNameYouWantToSet" 

Microsoft.Reporting.WinForms.ReportDataSource rs = new 
                Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);  


frm.reportViewer1.LocalReport.DataSources.Clear();
frm.reportViewer1.LocalReport.DataSources.Add(rs);
frm.reportViewer1.RefreshReport();
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58