FastReport icon indicating copy to clipboard operation
FastReport copied to clipboard

Issue with the setup with mongodb as datasource

Open chef-sudo opened this issue 6 years ago • 2 comments

I'm having a hard time with the setup as the documentation is limited and I can't find a proper demo for my needs.

Currently I'm trying to use the mongo db as my datasource but also create programmatically a template for my data.

The CreateTemplate was implemented based on this pdf tutorial

on the Startup > ConfigureServices I've added the FastReport.Utils.RegisteredObjects.AddConnection(typeof(MongoDBDataConnection));

private ReportPage CreateTemplate(Report report)
{
    ReportPage page = new ReportPage();
    page.CreateUniqueName();
    page.ReportTitle = new ReportTitleBand();
    page.ReportTitle.Height = 4.0f * Units.Centimeters;
    page.TopMargin = 10.0f;
    page.LeftMargin = 10.0f;
    page.RightMargin = 10.0f;
    page.BottomMargin = 10.0f;

    // Create report header
    TextObject titleText = new TextObject();
    titleText.CreateUniqueName();
    titleText.Left = 1.0f * Units.Centimeters;
    titleText.Top = 1.0f * Units.Centimeters;
    titleText.Width = 17.0f * Units.Centimeters;
    titleText.Height = 2.0f * Units.Centimeters;
    titleText.HorzAlign = HorzAlign.Center;
    titleText.VertAlign = VertAlign.Center;
    titleText.Font = new Font("Chiller", 22.0f, FontStyle.Bold);
    titleText.TextColor = Color.DarkGreen;
    titleText.FillColor = Color.DarkOrange;
    titleText.Border.Color = Color.DarkOrchid;
    titleText.Border.Lines = BorderLines.All;
    titleText.Border.Width = 4.0f;
    titleText.Text = "Report from code demo";
    page.ReportTitle.Objects.Add(titleText);

    // Create a page header
    TextObject headerText = new TextObject();
    headerText.CreateUniqueName();
    headerText.Bounds = new RectangleF(0.0f, 0.5f * Units.Centimeters, 19.0f * Units.Centimeters, 1.0f * Units.Centimeters);
    headerText.HorzAlign = HorzAlign.Center;
    headerText.VertAlign = VertAlign.Center;
    headerText.Font = new Font("Book Antique", 16.0f, FontStyle.Bold | FontStyle.Italic);
    headerText.TextColor = Color.Teal;
    headerText.FillColor = Color.YellowGreen;
    headerText.Border.Lines = BorderLines.All;
    headerText.Border.TopLine.Color = Color.Indigo;
    headerText.Border.LeftLine.Color = Color.Gold;
    headerText.Border.RightLine.Color = Color.Gold;
    headerText.Border.BottomLine.Color = Color.Indigo;
    headerText.Border.TopLine.Width = 3.0f;
    headerText.Border.LeftLine.Width = 2.0f;
    headerText.Border.RightLine.Width = 2.0f;
    headerText.Border.BottomLine.Width = 3.0f;
    headerText.Text = "Income Table";
    page.PageHeader = new PageHeaderBand();
    page.PageHeader.Objects.Add(headerText);

    // Create databand
    DataBand band = new DataBand();
    band.CreateUniqueName();
    band.DataSource = report.GetDataSource("Income");
    band.Height = 0.5f * Units.Centimeters;
    band.Top = 50 * i;

    TextObject idBandText = new TextObject();
    idBandText.CreateUniqueName();
    idBandText.Top = 50 * i;
    idBandText.HorzAlign = HorzAlign.Center;
    idBandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.0f,
    1.0f * Units.Centimeters, 1.5f * Units.Centimeters);
    idBandText.Border.Lines = BorderLines.All;
    idBandText.Text = "[Income.Id]";
    band.AddChild(idBandText);

    TextObject invoiceTypeBandText = new TextObject();
    invoiceTypeBandText.CreateUniqueName();
    invoiceTypeBandText.Top = 50 * i;
    invoiceTypeBandText.HorzAlign = HorzAlign.Center;
    invoiceTypeBandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.0f,
    1.0f * Units.Centimeters, 1.5f * Units.Centimeters);
    invoiceTypeBandText.Border.Lines = BorderLines.All;
    invoiceTypeBandText.Text = "[Income.InvoiceType]";
    band.AddChild(invoiceTypeBandText);

    TextObject issueDateBandText = new TextObject();
    issueDateBandText.CreateUniqueName();
    issueDateBandText.Top = 50 * i;
    issueDateBandText.HorzAlign = HorzAlign.Center;
    issueDateBandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.0f,
    1.0f * Units.Centimeters, 1.5f * Units.Centimeters);
    issueDateBandText.Border.Lines = BorderLines.All;
    issueDateBandText.Text = "[Income.IssueDate]";
    band.AddChild(issueDateBandText);

    page.Bands.Add(band);

    // Create page footer
    page.PageFooter = new PageFooterBand();
    page.PageFooter.CreateUniqueName();
    page.PageFooter.Height = 0.5f * Units.Centimeters;
    TextObject pageFooterText = new TextObject();
    pageFooterText.CreateUniqueName();
    pageFooterText.HorzAlign = HorzAlign.Right;
    pageFooterText.VertAlign = VertAlign.Center;
    pageFooterText.Bounds = new RectangleF(0.0f, 0.0f,
    19.0f * Units.Centimeters, 0.5f * Units.Centimeters);
    pageFooterText.TextColor = Color.Teal;
    pageFooterText.FillColor = Color.YellowGreen;
    pageFooterText.Border.Lines = BorderLines.All;
    pageFooterText.Border.TopLine.Color = Color.Indigo;
    pageFooterText.Border.LeftLine.Color = Color.Gold;
    pageFooterText.Border.RightLine.Color = Color.Gold;
    pageFooterText.Border.BottomLine.Color = Color.Indigo;
    pageFooterText.Border.TopLine.Width = 3.0f;
    pageFooterText.Border.LeftLine.Width = 2.0f;
    pageFooterText.Border.RightLine.Width = 2.0f;
    pageFooterText.Border.BottomLine.Width = 3.0f;
    pageFooterText.Text = "Page [Page]";
    page.PageFooter.Objects.Add(pageFooterText);

    // Create report footer
    page.ReportSummary = new ReportSummaryBand();
    page.ReportSummary.CreateUniqueName();
    page.ReportSummary.Height = 4.0f * Units.Centimeters;
    var reportFooterText = new TextObject();
    reportFooterText.CreateUniqueName();
    reportFooterText.Left = 1.0f * Units.Centimeters;
    reportFooterText.Top = 1.0f * Units.Centimeters;
    reportFooterText.Width = 17.0f * Units.Centimeters;
    reportFooterText.Height = 2.0f * Units.Centimeters;
    reportFooterText.HorzAlign = HorzAlign.Center;
    reportFooterText.VertAlign = VertAlign.Center;
    reportFooterText.Font = new Font("Chiller", 32.0f, FontStyle.Bold);
    reportFooterText.TextColor = Color.DarkGreen;
    reportFooterText.FillColor = Color.DarkOrange;
    reportFooterText.Border.Color = Color.DarkOrchid;
    reportFooterText.Border.Lines = BorderLines.All;
    reportFooterText.Border.Width = 4.0f;
    reportFooterText.Text = "Total Pages [TotalPages#]";
    page.ReportSummary.Objects.Add(reportFooterText);

    return page;
}

public async Task<string> GetReportByIdAsync(ObjectId id)
{
    ...

    using (var memoryStream = new MemoryStream())
    {
        try
        {
            using (var report = new Report())
            {
                // TODO: create singleton
                MongoDBDataConnection.dbName = "myDatabaseName";
                var conn = new MongoDBDataConnection();
                conn.ConnectionString = "mongodb://nosqldata";
                report.Dictionary.Connections.Add(conn);
                conn.CreateAllTables();


                report.Pages.Add(CreateTemplate(report));

                // Making the report preparation for the show
                report.Prepare();

                // Set the HTML builder options
                var htmlBuilder = new HTMLExport
                {
                    SinglePage = false, // report on the one page
                    Navigator = true, // navigation panel on top
                    EmbedPictures = true, // bwuild in images to the document
                    ExportMode = HTMLExport.ExportType.WebPreview
                };

                report.Export(htmlBuilder, memoryStream);

                htmlBuilder.Dispose();

                return memoryStream.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            // TODO: print exception
            return string.Empty;
        }
        finally
        {
            await memoryStream.DisposeAsync();

            if (frxMemoryStream != null)
            {
                await frxMemoryStream.DisposeAsync();
            }
        }
    }
}

Now, the CreateAllTables which btw idk what it does, it throws an error of

{System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at MongoDB.Driver.IAsyncCursorExtensions.First[TDocument](IAsyncCursor`1 cursor, CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.First[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
   at MongoDB.Driver.IFindFluentExtensions.First[TDocument,TProjection](IFindFluent`2 find, CancellationToken cancellationToken)
   at FastReport.Data.MongoDBDataConnection.CreateDataTable(DataTable table, Boolean allRows)
   at FastReport.Data.MongoDBDataConnection.FillTableSchema(DataTable table, String selectCommand, CommandParameterCollection parameters)
   at FastReport.Data.TableDataSource.InitSchema()
   at FastReport.Data.DataConnectionBase.CreateAllTables(Boolean initSchema)
   at FastReport.Data.DataConnectionBase.CreateAllTables()
   at eInvoice.WebSPA.Infrastructure.Services.Implementations.FastReportService.GetReportByIdAsync(ObjectId id) in ...

and if i comment it I get another error on the Prepare() of

{FastReport.Utils.CompilerException: (): Error CS0103: The name 'Income' does not exist in the current context
(): Error CS0103: The name 'Income' does not exist in the current context
(): Error CS0103: The name 'Income' does not exist in the current context

   at FastReport.Code.AssemblyDescriptor.InternalCompile()
   at FastReport.Code.AssemblyDescriptor.Compile()
   at FastReport.Report.Compile()
   at FastReport.Report.Prepare(Boolean append)
   at FastReport.Report.Prepare()
   at eInvoice.WebSPA.Infrastructure.Services.Implementations.FastReportService.GetReportByIdAsync(ObjectId id) in ...

The app is a Blazor .net core 3, Any help is appreciated!

chef-sudo avatar Aug 12 '20 14:08 chef-sudo

I've checked MongoDb connection right now and it works on my machine. MongoDBDataConnection.dbName = "myDatabaseName"; - the dbName is supposed to be a Collection name

Also you should enable your data source before using it in your report. Check this, please - https://www.fast-report.com/en/blog/89/show/

8VAid8 avatar Aug 19 '20 09:08 8VAid8

Thanks for the clarification! I'll check it back when I'll return from vacation!

chef-sudo avatar Aug 19 '20 11:08 chef-sudo