Dynamically adding a subreport to a page

Stimulsoft Reports.NET discussion
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Dynamically adding a subreport to a page

Post by prajan »

Hi,
In my web page I am trying to generate a report, where I need to dynamically add the components. I am not able to add the sub report. I am getting the following exception while trying to render the report :
Object reference not set to an instance of an object.
I know it has something to do with the subreport 'cos in the exception in the 'Target site' I get the following message :
{Stimulsoft.Report.Components.StiContainer RenderInternalSubReport(Stimulsoft.Report.Components.StiSubReport)}

Following is my code. Please let me know if I am missing something.

Code: Select all

public partial class Default2 : System.Web.UI.Page
{
    private StiReport report = new StiReport();

    protected void Page_Load(object sender, EventArgs e)
    {
        StiReport report = CreateReport1();
        report.Render();        // This is the point of exception 
        StiWebViewer1.Report = report;      
    }

    private StiReport CreateReport1()
    {
        StiReport report = new StiReport();
        StiPage BSReport = report.Pages[0];
        BSReport.PaperSize = System.Drawing.Printing.PaperKind.Ledger;

        // Add the main databand
        StiDataBand dbMain = new StiDataBand();
        dbMain.Height = 0.4;
        dbMain.Name = "stidbMain";
        BSReport.Components.Add(dbMain);

        double subrepsize = BSReport.Width / 2;

        StiPage LiabRep = new StiPage();
        report.Pages.Add(LiabRep);

        StiSubReport SubRep1 = new StiSubReport(new RectangleD(0, 0, subrepsize, 0.4));
        SubRep1.Name = "subLiabRep";
        SubRep1.Page = LiabRep;
        dbMain.Components.Add(SubRep1);

        cs_GeneralLedger.cs_GLReports objBalanceSheetRep = new cs_GeneralLedger.cs_GLReports();
        objBalanceSheetRep.Login = (string)Session["UserID"];
        objBalanceSheetRep.LoginPassword = (string)Session["Password"];

        cs_ConnectionString ConStr = new cs_ConnectionString();

        DataSet dsBSReport = objBalanceSheetRep.m_BalanceSheetReport(11, "26/5/2013", "GL", true, "abc");

        dsBSReport.Tables[0].TableName = "dtBSRepTitleDetails";
        DataTable dtRepDet = dsBSReport.Tables[0];

        dsBSReport.Tables[1].TableName = "dtBSUsersPCList";
        DataTable dtUserPC = dsBSReport.Tables[1];

        dsBSReport.Tables[2].TableName = "dtBSLiabs";
        DataTable dtRepLiab = dsBSReport.Tables[2];

        dsBSReport.Tables[3].TableName = "dtBSAssets";
        DataTable dtRepAsst = dsBSReport.Tables[3];

        report.RegData(dsBSReport);
        report.Dictionary.Synchronize();


        //Create TitleBand
        StiHeaderBand TitleBand = new StiHeaderBand();
        TitleBand.Height = 0.85;
        TitleBand.Name = "TitleBand";
        LiabRep.Components.Add(TitleBand);

        //Create HeaderBand
        StiHeaderBand headerBand = new StiHeaderBand();
        headerBand.Height = 0.5;
        headerBand.Name = "HeaderBand";
        LiabRep.Components.Add(headerBand);

        //Create Databand
        StiDataBand dataBand = new StiDataBand();
        dataBand.DataSourceName = "dtBSLiabs";
        dataBand.Height = 0.5;
        dataBand.Name = "DataBand";
        LiabRep.Components.Add(dataBand);

        double pos = 0;
        double columnWidth = LiabRep.Width / ((dtUserPC.Rows.Count + 1) * 2);
        double x = columnWidth * 2; 

        StiText hTextL = new StiText(new RectangleD(pos, 0, x, 0.7));
        hTextL.Text.Value = "Liabilities";
        hTextL.HorAlignment = StiTextHorAlignment.Center;
        hTextL.VertAlignment = StiVertAlignment.Center;
        hTextL.Name = "HeaderTextL";
        hTextL.Border.Side = StiBorderSides.All;
        headerBand.Components.Add(hTextL);

        pos = pos + x;
        int nameIndex = 1;        
        foreach (DataRow drUserPC in dtUserPC.Rows)
        {   
            StiText hText = new StiText(new RectangleD(pos, 0, x, 0.4));
            //hText = new StiText(new RectangleD(pos, 0, x, 0.7));

            hText.Text.Value = drUserPC["ProfitCenter"].ToString() + System.Environment.NewLine + "Amt in Rs.";
            hText.HorAlignment = StiTextHorAlignment.Center;
            hText.VertAlignment = StiVertAlignment.Center;
            hText.Name = "HeaderText" + nameIndex.ToString();
            hText.Border.Side = StiBorderSides.All;
            headerBand.Components.Add(hText);

            StiText hText1 = new StiText(new RectangleD(pos, 0.4, columnWidth, 0.3));
            hText1.Text.Value = "Dr";
            hText1.HorAlignment = StiTextHorAlignment.Center;
            hText1.VertAlignment = StiVertAlignment.Center;
            hText1.Name = "HeaderText1" + nameIndex.ToString();
            hText1.Border.Side = StiBorderSides.All;
            headerBand.Components.Add(hText1);

            StiText hText2 = new StiText(new RectangleD(pos + columnWidth, 0.4, columnWidth, 0.3));
            hText2.Text.Value = "Cr";
            hText2.HorAlignment = StiTextHorAlignment.Center;
            hText2.VertAlignment = StiVertAlignment.Center;
            hText2.Name = "HeaderText2" + nameIndex.ToString();
            hText2.Border.Side = StiBorderSides.All;
            headerBand.Components.Add(hText2);

            nameIndex++;
            pos = pos + x;
        }

        return report;
    }
}

Thanks in advance
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamically adding a subreport to a page

Post by Alex K. »

Hello,

Please send us a sample project with test data for analysis.

Thank you.
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Re: Dynamically adding a subreport to a page

Post by prajan »

PFA the data for the report and the page where I am trying to generate the report dynamically.
I need to know how to add a subreport into the report dynamically.

Please let me know.

Thanks
Attachments
Default2.aspx.cs
code behind file
(5.24 KiB) Downloaded 313 times
Data for Rep.xlsx
Data for report
(12.1 KiB) Downloaded 236 times
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Re: Dynamically adding a subreport to a page

Post by prajan »

Do we have any updates on this please ?
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Re: Dynamically adding a subreport to a page

Post by prajan »

Hi,
Any time frame by when I can expect a response on this?

Thanks in advance.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamically adding a subreport to a page

Post by Alex K. »

Hello,

Please try to set the SubReportPageGuid property for SubReport.

Code: Select all

SubRep1.SubReportPageGuid = LiabRep.Guid;
Thank you.
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Re: Dynamically adding a subreport to a page

Post by prajan »

Hi,
Thanks. That did help.
But I have a new query now. How to add highlight conditions dynamically to a text field.

Thanks in advance.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamically adding a subreport to a page

Post by Alex K. »

Hello,

You can create a report in the designer, open the Code tab and use the similar code in your application.

Thank you.
prajan
Posts: 49
Joined: Tue Apr 23, 2013 1:28 pm

Re: Dynamically adding a subreport to a page

Post by prajan »

Hi,
Can you please tell me how we can calculate the group header band sum value dynamically?
In the auto generated code the entire dictionary and all the columns in it are declared, but in my case, the number of columns in the data tables are not constant, so I cant define the entire dictionary and hence cannot define the conditions as declared in the auto generated code.
Please help me to calculate the sum of the group header band.

Thanks
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamically adding a subreport to a page

Post by Alex K. »

Hello,

As a way you can use the following code:

Code: Select all

StiText textComp = report.GetComponents()["Text2"] as StiText;
textComp.Text = new StiExpression("{Totals.Sum(GroupHeaderBand1, Products.UnitPrice)}");
Thank you.
Post Reply