How to change the connection string of Subreport programatically?

Posted by: p_ffung on 22 November 2020, 2:14 pm EST

  • Posted 22 November 2020, 2:14 pm EST

    Hi,

    In .NET Core 3.1 project, I want to change the connection string of a Report, and also its Subreport(s), i found that i could use below code to overwriten the original one :

    String connStrAR = "...myConnectionString"
    rpt.Report.DataSources[0].DataSourceReference = "";
    rpt.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
    rpt.Report.DataSources[0].ConnectionProperties.ConnectString = connStrAR;

    But i also found that i cant change its subreport connection string.
    How could i do it?

    Thanks
  • Replied 23 November 2020, 6:28 pm EST

    Hello,

    Can you please try with the following line of code:
    GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();
    GrapeCity.ActiveReports.PageReportModel.Subreport rs = (GrapeCity.ActiveReports.PageReportModel.Subreport)rpt.Report.Body.ReportItems["SubRReport"];
    GrapeCity.ActiveReports.PageReport subreport = new GrapeCity.ActiveReports.PageReport(new FileInfo(rs.ReportName));
    subreport.Report.DataSources[0].DataSourceReference = "";
    subreport.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
    subreport.Report.DataSources[0].ConnectionProperties.ConnectString = connStrAR;


    Hope it helps.

    Thanks,
    Mohit
  • Replied 3 December 2020, 8:24 pm EST

    Hi Mohit,

    I tried your code, but my Subreport is placed inside a "Section" of "PageHeader",
    how should i access this report?

    Franky
  • Replied 3 December 2020, 8:38 pm EST

    Hi,

    I correct my question:
    The connection string of Report is already set,
    is there a way to automatically apply this connection string to SubReport too?

    I understood the above code works for modifying SubReport's connection string, but it would troublesome to specify each SubReport name or loop through the report to lookup and modify the connection string.

    Best if there would be a way to apply this connection string just once.

    Franky
  • Replied 7 December 2020, 1:34 am EST

    Hello Franky!

    Could you please confirm your use-case. You are using the JSViewer to preview the report in Asp.Net Core application. And, you are using the "UseCustomStore" to change the connection string of the MainReport. Now, you want a way to change the connection string of the SubReport for both SectionReport and RDL report.

    Request to please confirm if my understanding is right?

    Thanks,
    Mohit
  • Replied 7 December 2020, 1:34 am EST

    Hello Franky!

    Could you please confirm your use-case. You are using the JSViewer to preview the report in Asp.Net Core application. And, you are using the "UseCustomStore" to change the connection string of the MainReport. Now, you want a way to change the connection string of the SubReport for both SectionReport and RDL report.

    Request to please confirm if my understanding is right?

    Thanks,
    Mohit
  • Replied 8 December 2020, 4:25 pm EST

    Hi Mohit,

    I am developing a Asp.Net Core application, but not using JSViewer, instead, i used below code to generate report :



    string rptFile = "rpt0001.rdlx";

    GrapeCity.ActiveReports.PageReport rpt = new PageReport(new System.IO.FileInfo(rptFile));
    GrapeCity.ActiveReports.Document.PageDocument pageDocument = new GrapeCity.ActiveReports.Document.PageDocument(rpt);

    GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();

    GrapeCity.ActiveReports.Export.Pdf.Page.Settings pdfSettings = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();
    GrapeCity.ActiveReports.Extensibility.Rendering.IRenderingExtension renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
    rpt.Document.Render(renderingExtension, outputProvider, pdfSettings);



    I only need a way to change the connection string of the SubReport for RDL report
  • Marked as Answer

    Replied 9 December 2020, 2:57 pm EST

    Hello Franky,

    Thanks for the confirmation, You can do the following thing:
    1: Create a SubReport
    2: Add a Parameter and make it hidden
    3: Set the ConnectionString value to above added parameter
    4: Create a Main Report and Set the above subReport using the SubReport control
    5: Add a parameter in the MainReport
    6: Map the MainReport parameter with the SubReport parameter as shown in the attached image:
    7: After that you can pass the Parameter value to MainReport at runtime using the following line of code:
    foreach (PageReportModel.ReportParameter parameter in pageReport.Report.ReportParameters)
    {
    if (parameter.Name == "P1")
    {
    parameter.DefaultValue.Values.Clear();
    parameter.DefaultValue.Values.Add("Test & Test");
    }
    }


    Please refer to the attached reports.

    Hope it helps.

    Thanks,
    Mohit

    SubReportTest.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels