3
votes

I developed an SSRS report having a main and 3 subreport. I am calling this report from C#. I only know how to bind the main rdlc with the data set.

I use the code below for this

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
dataAdapter.Fill(dataset);
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\sale_dept.rdl";
 this.reportViewer1.LocalReport.DataSources.Clear();
 this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.RefreshReport();

when I run the exe i am getting the report viewer filled with main report but 3 subreport shows error because i didn't specify the DataSource for those subreports

enter image description here

  1. There is no parameter passing between the main and other sub reports
  2. The dataset name for the main and all sub report is default as DataSet1

Please guide me to bind the sub reports with appropriate query dataset tables. I am totally stuck here.

Edited

I changed my project with 1 subreport.

In SSRS it is working fine in (the BIDS) editor but when calling from C# it is giving error:

Could not be found at the specified location. Please verify that the subreport has been published and that the name is correct.

My code:

subreportevenhandler according to this question

question for subreport event handler

 SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
    dataAdapter.Fill(dataset);
    this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\sale_dept.rdl";
     this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.SubreportProcessing +=
                new SubreportProcessingEventHandler(addsubreport);
     this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
    this.reportViewer1.RefreshReport();




void addsubreport(object sender, SubreportProcessingEventArgs e)
        {
            SqlConnection conn = new SqlConnection(source);
            DataSet dataset = new DataSet();
            conn.Open();

           SqlCommand sqlcomm = new SqlCommand( "Query for subreport", conn);

           SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
           dataAdapter.Fill(dataset);

           e.DataSources.Add(new ReportDataSource("DataSet1", dataset.Tables[0]));
        }

Still I am getting error for subreport I moved all the .rdl file to C# bin folder..

enter image description here

Main report is showing the data correctly. In SSRS its fine..

1
And the sub-report controls (eg Tablix) are bound to the Dataset1s fields? Is it a shared dataset, how do you have it configured?Jeremy Thompson
@JeremyThompson no each report has its on query and dataset...but the datasetname for all report is Dataset1Sachu
@JeremyThompson yes..but in SSRS my report is working fine because no parameter is passing..the query is hard code..the query is build in C# during execution and the datatable filled by dataadapter is given as datasource for the report..Sorry if i am wrong..i naive to this SSRSSachu
@Sachu In your RDL when calling the subreport give the absolute path instead of relative path. SubReport Properties > General > Use this report as subreportAnup Agrawal

1 Answers

3
votes

I found out the issue. I am posting it as answer because it may help someone in future.

The SubreportProcessingEventHandler will only get fired for .rdlc subreports. In my project main report and all subreports are .rdl extension. So only change I done was went to command prompt and renamed the subreport extension as .rdlc

eg:- ren discount.rdl discount.rdlc

Then attach the data-set for the sub-report accordingly.

See the code as below

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
dataAdapter.Fill(dataset);
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\main_rpt.rdl";
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(addsubreport);
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.RefreshReport();


void addsubreport(object sender, SubreportProcessingEventArgs e)
{

SqlCommand sqlcomm = new SqlCommand();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataset = new DataSet();

Switch(e.ReportPath)
{
case "subreport1":
   sqlcomm = new SqlCommand( "Query for subreport one", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport1", dataset.Tables[0]));
   break;
case "subreport2":
   sqlcomm = new SqlCommand( "Query for subreport two", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport2", dataset.Tables[0]));
   break;
case "subreport3":
   sqlcomm = new SqlCommand( "Query for subreport three", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport3", dataset.Tables[0]));
   break;

 }

}

Switch is needed if you have more than one sub-report. Main point to notice

  1. All sub-report should have extension .rdlc

2.if any parameter is passing make sure it is also named correctly.

  1. Specify the path correctly. It's better to put main report and subreport in same folder.

Now run the C# application it will show the main report with all sub-report