[ACCEPTED]-How can I load datatable as ReportDataSource?-reportviewer

Accepted answer
Score: 10

You are not initializing the ReportDataSouce 5 correctly. Give this a try:

DataTable dt = new DataTable(); 
dt = this.inputValuesTableAdapter.GetData();     

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


Also, you might 4 need to alter the first parameter to the 3 ReportDataSource constructor to set the 2 name of the datasource that your report 1 is expecting.

Score: 4

I believe that madisonw's answer above is 17 correct, but Luke's comment about using 16 the DataSet name as named in the .rdlc report file, for the Datasource.Name 15 property, perhaps needs to be emphasized. For 14 me this was the main bugaboo that kept my 13 app from working. It can be found by opening 12 the .rdlc file as an XML file by using the 11 "Open with..." command. The default I think 10 is simply "DataSet1":

  <DataSet Name="DataSet1">
      <Field Name="BusinessEntityID">

One other mistake I 9 made was not including the .rdlc file in 8 the Debug (or Release) folder. This was 7 fixed by right-clicking on the .rdlc file 6 in Solution Explorer, then Properties, then 5 setting "Copy to Output Directory" to "Copy 4 Always".

Once these two parts were corrected 3 my program worked to use ReportViewer within 2 a console app to generate a PDF file with 1 no interface.

Score: 2
reportViewer1.LocalReport.ReportEmbeddedResource = "Your Report Name.rdlc";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Connection";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from YourTableName";
DataTable dt = new DataTable();
ReportDataSource rprtDTSource= new ReportDataSource();
rprtDTSource.Name = "reportDataSetName";
rprtDTSource.Value = dt;


Score: 0

Imagine the DataSources block of your RDLC 5 file is like below:

< DataSets > < DataSet 4 Name="DataSet1_Lot" >

then the relative code 3 should be:

string name = "DataSet1_Lot"; // this must exist in the RDLC file DataTable 2 dt = new DataTable();

Microsoft.Reporting.WinForms.ReportDataSource 1 rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);

More Related questions