FastReports / FastReport

Free Open Source Reporting tool for .NET6/.NET Core/.NET Framework that helps your application generate document-like reports
https://www.fast-report.com
MIT License
2.71k stars 603 forks source link

Scripting inside report #176

Closed drechema closed 4 years ago

drechema commented 4 years ago

Hello,

I am running netcore 3 in Mac OSX

I made a simple report using Designer Community following documentation. The Script C# looks like this inside the .frx file:

<ScriptText>using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport
{
  public class ReportScript
  {
    private void EstadoResultado_BeforePrint(object sender, EventArgs e)
    {
      if (((Int16)Report.GetColumnValue(&quot;demo&quot;)) &gt; 0) {
        Demo.TextColor = Color.Red;
      }
    }
  }
}
</ScriptText>

The report works fine in Preview from Designer Community.

I developed a simple Web API project using netcore 3.1 following one of your samples. The API use PDFSimpleExport to generate a PDF from report:

            report.Prepare();
            PDFSimpleExport pdf = new PDFSimpleExport();
            MemoryStream memStream = new MemoryStream();
            report.Export(pdf, memStream);

This code works perfectly with a report with no Scripting. But if I try with the before scripted report I have:

An unhandled exception has occurred while executing the request.
FastReport.Utils.CompilerException: (21,24): Error CS0012: The type 'Color' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing.Primitives, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(21,36): Error CS0103: The name 'Color' 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()

How can we add the reference to that assembly? How can we add the needed assembly referenced in the Report Script? Everything inside the Script is pure NetCore, I mean will run in any paltform?

Thanks

drechema commented 4 years ago

Tested with netcore2.1 framework and get same error

drechema commented 4 years ago

I also try in Windows and get same error

KirillKornienko commented 4 years ago

Hello! We fixed this error in the last MR https://github.com/FastReports/FastReport/pull/178. Nuget package version: 2020.2.9 (https://www.nuget.org/packages/FastReport.OpenSource/2020.2.9). Please, try this changes and share your opinion.

drechema commented 4 years ago

Hello @KirillKornienko, I have tested with release 2020.2.9 and works perfect !! Thanks a lot for your help

But after ... I tried to make more in the Script and I get this new error :-(

Exception has occurred: CLR/FastReport.Utils.CompilerException
An unhandled exception has occurred 'FastReport.Utils.CompilerException' in FastReport.dll
(83,22): Error CS0012: The type 'CollectionBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections.NonGeneric, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(89,29): Error CS0012: The type 'IDynamicMetaObjectProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(109,21): Error CS0012: The type 'CollectionBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections.NonGeneric, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(89,8): Error CS0012: The type 'IDynamicMetaObjectProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(116,22): Error CS0012: The type 'CollectionBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections.NonGeneric, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'
   en FastReport.Code.AssemblyDescriptor.InternalCompile()
   en FastReport.Code.AssemblyDescriptor.Compile()
   en FastReport.Report.Compile()
   en FastReport.Report.Prepare(Boolean append)
   en FastReport.Report.Prepare()

It seems quite similar problem... I don't know if I am doing something wrong...

KirillKornienko commented 4 years ago

Please, send us your sample report (*.frx) with this bug.

drechema commented 4 years ago

I have prepared a .frx that throw this error demo-win.frx.zip

I only added a function dealing with lists and arrays like this one:

   private void AddTable(List<string[]> table){
      TableObject tbl = new TableObject();
      for (int icol=0;icol<4;icol++){
        TableColumn col = new TableColumn();
        tbl.Columns.Add(col);          
      }
      foreach (var row in table){
        TableRow tr = new TableRow();
        tr.AutoSize = true;
        for (int icol=0;icol<4;icol++) {
          TableCell tc = new TableCell();
          tc.Text = row[icol];
          tr.AddChild(tc);        
        }
        tbl.Rows.Add(tr);
      }
      tbl.Bounds = new RectangleF(NombrePrueba.Left, NombrePrueba.Bottom, Units.Centimeters * 10, Units.Centimeters * 0.5f);
      tbl.CanGrow = true; 
      tbl.Top = 18;
      DatosOrden.Objects.Add(tbl);    
    }

Thanks!

drechema commented 4 years ago

The above report runs fine form the designer (well... it's no pretty but works) but throw the error at Prepare method if is launched from code

KirillKornienko commented 4 years ago

Hello! In the latest update 2020.3.0, we fixed this problem. Please, try this changes and share your opinion.

KirillKornienko commented 4 years ago

Hello @drechema. Yes, dotnet add package FastReport.Compat --version 2020.3.2 before OpenSource version. This package is specified in the dependencies FastReport.OpenSource. It's strange that it does not load automatically.

drechema commented 4 years ago

Yes!! It's working After install this packages

dotnet add package FastReport.OpenSource --version 2020.3.1
dotnet add package FastReport.Compat --version 2020.3.2

the project is running and the report is showed !! Thanks a lot !!

drechema commented 4 years ago

@KirillKornienko everything works good but let me go ahead. What I need if a want to use another third party library inside the Report?

I tested adding:

using Newtonsoft.Json.Linq;

And then I have an exception:

An unhandled exception has occurred while executing the request.
FastReport.Utils.CompilerException: (13,6): Error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

   at FastReport.Code.AssemblyDescriptor.InternalCompile()

But Newtonsoft.Json.dll exist in the bin directory. Should I include the library in the project using nuget or similar?

Thanks again for your work

KirillKornienko commented 4 years ago

@drechema In the last update we made the automatic addition of assembly if they are known to .Net. In case of unknown assemblies, please add (do not replace) them in Report.ReferencedAssemblies before preparing the report (before Report.Prepare()).

drechema commented 4 years ago

Thanks, I think that the issue is closed !

ghost commented 2 years ago

<!DOCTYPE html>

Document   | Name | Value | Type -- | -- | -- | -- ◢ | $exception | {"(0,0): Error CS0006: Metadata file 'Microsoft.CSharp' could not be found\r\n"} | FastReport.Utils.CompilerException

Getting this error in 2022.2.11(regression?) on report.Prepare First time trying fast reports, not a happy experience with the opensource, its very difficult to find the code examples that work. ?? going to try the older version or get the open source code and compile

ghost commented 2 years ago

sorry used nuget and got opensource and compat Using winForms .net framework 4.6.1 and vs2022 ... trying to replace winforms reporting so I can upgrade it. 2021.3.1 appears to work for me.

AhmedSabry45 commented 8 months ago

I've built out a couple reports, and I'm able to test them outside of the application, but when I come into the application and go to Preview a Report, I'm getting an error:

I think that's probably because there's an error in an expression in the text object called Text2, and it doesn't like something about the Prefix field from one of my Data Sources called "Data" but I can't find any reason why that would be a problem. I'm sure it's probably some small setting I am unaware of, but I just don't know what that would be. The field is in the Report Title section of the report, and when I remove it, the error changes to the next text object that appears in the next section.