locked
Report Viewer didn't call sub Report method RRS feed

  • Question

  • User-1614814468 posted

    I use following code to generate a PDF report:

    public partial class Report{   
     public void myMethode(DataTable DT){
    
            ReportViewer reportViewer1 = new ReportViewer();
            reportViewer1.LocalReport.SetBasePermissionsForSandboxAppDomain(new PermissionSet(PermissionState.Unrestricted));
            reportViewer1.LocalReport.ReportPath = @"Something" + ReportName + ".rdlc";
            reportViewer1.ProcessingMode = ProcessingMode.Local;
    
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", DT));
            reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubReportProcessing);
    
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
    
       }
     private static void SubReportProcessing(object sender, Microsoft.Reporting.WebForms.SubreportProcessingEventArgs e)
       {
            //do something
       }
    
    }

    It's work fine for every main report but when it want to generate subreport it didn't call SubReportProcessing method!!!

    So can some one help me to find problem??

    I am using Dot Net 4.7.2 also I used this as web api and I use Microsoft.ReportViewer.WebFormsversion 12.0.0.0

    Monday, May 27, 2019 12:41 PM

All replies

  • User-1174608757 posted

    Hi amir133,

    According to your codes,I suggest that you could use SubreportProcessingEventHandler to call subreport,you could write you method as below:

    this.reportViewer.Reset();
    this.reportViewer1.LocalReport.DataSources.Add(datasource);
    this.reportViewer1.LocalReport.ReportPath =".."; this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(addsubreport); 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; }

    Best Regards

    Wei

    Tuesday, May 28, 2019 6:04 AM
  • User-1614814468 posted

    Thanks Wei Zhang. I will try your answer.

    but before that I have a question:
    Is it necessary to Create a Form and call reportViewer's component?
    If not, then there is no problem with creating instance from ReportViwer?
    Because I feel I need to use its component.

    Tuesday, May 28, 2019 6:24 AM
  • User-1174608757 posted

    Hi amir133,

    amir133

    Is it necessary to Create a Form and call reportViewer's component?

    I think it is necessary,in application, you need a form to submit the data in code behind and basically rdlc file is a source , you still need a reportviewer controller to show the data in browser.So you need a form to submit data and set the attribute.

    Best Regards

    Wei

    Tuesday, May 28, 2019 8:14 AM
  • User-1614814468 posted

    If I do not want to display the result of the report to the user or provide the user with a report file, do I need to still have a ReportViewer controller?

    thanks ....

    Tuesday, May 28, 2019 11:43 AM
  • User-1174608757 posted

    Hi amir133,

    If I do not want to display the result of the report to the user or provide the user with a report file, do I need to still have a ReportViewer controller?

    In this situation, you don't need to use a ReportViewer controller , this control is a display of the data, if you just want to provide the original file, then of course you do not need to use the control.

    Best Regards

    Wei

    Wednesday, May 29, 2019 1:28 AM
  • User-1614814468 posted

    I test your solution but it shows this exception:'reportViewer1.Controls' threw an exception of type 'Microsoft.Reporting.WebForms.ScriptManagerNotFoundException'

    Wednesday, May 29, 2019 8:06 AM
  • User-1174608757 posted

    Hi amir133,

    According to error message ,if you get this error after add a new report viewer to a web page, you must add a ScriptManager from “AJAX Extensions” in the toolboox. This will correct this error.

    Best Regards

    Wei

    Wednesday, May 29, 2019 8:27 AM
  • User-1614814468 posted

    Thanks Wei Zhang,

    But  I do not add any report viewer to any page.

    my code is that I post for you at the first.

    See this picture :
    https://drive.google.com/file/d/1KaxQC6YNAuIZ__oiarTtZL0obhArxZ65/view?usp=sharing

    Sunday, June 2, 2019 6:31 AM
  • User-1174608757 posted

    Hi amir133,

    Ok , you could also try to add a a ScriptManager from “AJAX Extensions” in the toolboox.It depends on the error message. 

    Best Regards

    Wei

    Monday, June 3, 2019 5:19 AM