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.61k stars 582 forks source link

Can't write CLR type FastReport.Variant with handler type TextHandler #617

Closed Qulava closed 6 months ago

Qulava commented 10 months ago

Hello. Can you help me with my problem. I'm trying use my report from c# code, but i get some error "Can't write CLR type FastReport.Variant with handler type TextHandler". When i use it in designer, it works fine, but i cant understand what i do wrong using it in code. I tried to ask google, and found similar questions, but they were left unanswered. If I missed an existing issue, please give a link.

Im using .net 7 and PostgreSQL 12

Packages

  1. Npgsql.EntityFrameworkCore.PostgreSQL.nupkg v 7.0.4
  2. FastReport.OpenSource.Export.PdfSimple.2023.2.0.nupkg
  3. FastReport.Data.Postgres.2023.2.0.nupkg
  4. FastReport.OpenSource.2023.2.0.nupkg

Report exmaple:

<?xml version="1.0" encoding="utf-8"?>
<Report ScriptLanguage="CSharp" ReportInfo.Created="02/20/2023 14:07:45" ReportInfo.Modified="08/06/2023 22:23:11" ReportInfo.CreatorVersion="2023.2.0.0">
  <Styles Name="Standard">
    <Style Name="Title" Font="Arial, 12pt, style=Bold"/>
    <Style Name="Header" Font="Arial, 10pt, style=Bold"/>
    <Style Name="Group" Font="Arial, 10pt, style=Bold"/>
    <Style Name="Data" Font="Arial, 10pt"/>
    <Style Name="Footer" Font="Arial, 10pt"/>
    <Style Name="EvenRows" Fill.Color="WhiteSmoke" Font="Arial, 10pt"/>
  </Styles>
  <Dictionary>
    <PostgresDataConnection Name="Connection" ConnectionString="rijcmlqYZ85QOTN0CB5BIiFbZqEELZL+S/SfFJ3AH+UYEnRu7HVtXZ/LWLsvO6LVB07f43JMp3Q57uaKQkTVaBPwlRD+9/TYe2qYj6Cjunpr5L67iQ=">
      <TableDataSource Name="Table" Alias="certificate_query" DataType="System.Int32" Enabled="true" SelectCommand="select &#13;&#10;   &quot;Id&quot; as id, &#13;&#10;    &quot;Number&quot; as number,&#13;&#10; &quot;FullName&quot; as full_name, &#13;&#10;   &quot;UpdateDate&quot; as date, &#13;&#10;  &quot;Address&quot; as address  &#13;&#10;from &#13;&#10;   &quot;Certificates&quot; c&#13;&#10;where   &#13;&#10;  &quot;Status&quot; ='3'&#13;&#10;   and&#13;&#10;   cast (&quot;Id&quot; as varchar) = @id_spr">
        <Column Name="id" DataType="System.Guid"/>
        <Column Name="address" DataType="System.String"/>
        <Column Name="date" DataType="System.DateTime"/>
        <Column Name="number" DataType="System.Int32"/>
        <Column Name="full_name" DataType="System.String"/>
        <CommandParameter Name="id_spr" DataType="22" DefaultValue="4262186f-ead0-4fd8-847f-d69dc7579167"/>
      </TableDataSource>
    </PostgresDataConnection>
    <Parameter Name="id_spr" DataType="System.String"/>
  </Dictionary>
  <ReportPage Name="Page1" Landscape="true" PaperWidth="210.1" PaperHeight="148.1" RawPaperSize="11" Watermark.Font="Arial, 60pt" LastPageSource="15" FirstPageSource="15">
    <PageHeaderBand Name="PageHeader1" Width="718.58" Height="340.2">
      <TextObject Name="Text1" Width="718.2" Height="18.9" Text="Number № [certificate_query.number]" HorzAlign="Center" Font="Times New Roman, 14pt, style=Bold"/>
      <TextObject Name="Text2" Left="9.45" Top="47.25" Width="66.15" Height="18.9" Text="Issued: " Font="Times New Roman, 12pt"/>
      <TextObject Name="Text4" Left="75.6" Top="47.25" Width="576.45" Height="18.9" Text="[certificate_query.full_name]" Font="Times New Roman, 12pt, style=Bold, Underline"/>
      <TextObject Name="Text6" Left="198.45" Top="85.05" Width="453.6" Height="47.25" Text="[certificate_query.address]" Font="Times New Roman, 12pt, style=Bold, Underline"/>
    </PageHeaderBand>
  </ReportPage>
</Report>

Adding FastReportService

services.AddScoped<IReportService, FastReportService>();
FastReport.Utils.RegisteredObjects.AddConnection(typeof(PostgresDataConnection), "Connection");

Call report

public Stream MakeReportStream(string fileName, Dictionary<string, string> parameters = null)
{
    using var report = new Report();

    report.Load(fileName);

    foreach (var parameter in parameters)
    {
        report.SetParameterValue(parameter.Key, parameter.Value);
    }

    report.Prepare();

    using var pdfExport = new PDFSimpleExport();

    var stream = new MemoryStream();

    pdfExport.Export(report, stream);

    stream.Position = 0;

    return stream;
}

Error description

{
  "status": false,
  "response": "",
  "errorInfo": {
    "exceptionType": "System.InvalidCastException",
    "message": "Can't write CLR type FastReport.Variant with handler type TextHandler",
    "error": "(   at Npgsql.Internal.TypeHandlers.TextHandler.ValidateObjectAndGetLength(Object value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter)\r\n   at Npgsql.NpgsqlParameter.ValidateAndGetLength()\r\n   at Npgsql.NpgsqlParameterCollection.ProcessParameters(TypeMapper typeMapper, Boolean validateValues, CommandType commandType)\r\n   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)\r\n   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)\r\n   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)\r\n   at FastReport.Data.DataConnectionBase.FillTableData(DataTable table, String selectCommand, CommandParameterCollection parameters) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataConnectionBase.cs:line 820\r\n   at FastReport.Data.DataConnectionBase.FillTable(TableDataSource source) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataConnectionBase.cs:line 876\r\n   at FastReport.Data.TableDataSource.LoadData(ArrayList rows) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\TableDataSource.cs:line 288\r\n   at FastReport.Data.DataSourceBase.LoadData() in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 430\r\n   at FastReport.Data.DataSourceBase.Init(Relation relation, String filter, SortCollection sort, Boolean useAllParentRows) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 610\r\n   at FastReport.Data.DataSourceBase.Init(DataSourceBase parentData, String filter, SortCollection sort, Boolean useAllParentRows) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 594\r\n   at FastReport.Data.DataSourceBase.Init(DataSourceBase parentData, String filter, SortCollection sort) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 572\r\n   at FastReport.Data.DataSourceBase.Init(String filter, SortCollection sort) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 542\r\n   at FastReport.Data.DataSourceBase.Init(String filter) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 531\r\n   at FastReport.Data.DataSourceBase.Init() in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 522\r\n   at FastReport.Data.DataSourceBase.get_Item(Column column) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\DataSourceBase.cs:line 164\r\n   at FastReport.Data.Column.get_Value() in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Data\\Column.cs:line 256\r\n   at FastReport.Report.GetColumnValue(String complexName, Boolean convertNull) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Report.cs:line 1533\r\n   at FastReport.Report.GetColumnValue(String complexName) in C:\\Users\\Hontor\\Desktop\\FastReport-2023.2.0\\FastReport.Base\\Report.cs:line 1561\r\n   at TForum.Domain.Services.Reports.FastReportService.MakeReportStream(String fileName, Dictionary`2 parameters) in D:\\market\\tforum-old\\app.source\\TForum.Domain\\Services\\Reports\\FastReportService.cs:line 36\r\n   at TForum.Api.Controllers.ReportController.Test(Nullable`1 id) in D:\\market\\tforum-old\\app.source\\TForum.Api\\Controllers\\ReportController.cs:line 37\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n   at TForum.Api.Infrastructure.Handlers.CustomExceptionHandler.Invoke(HttpContext context) in D:\\market\\tforum-old\\app.source\\TForum.Api\\Infrastructure\\Handlers\\CustomExceptionHandler.cs:line 34)\r\n"
  }
}

Query from report

select 
    "Id" as id, 
    "Number" as number,
    "FullName" as full_name, 
    "UpdateDate" as date, 
    "Address" as address  
from 
    "Certificates" c
where   
    "Status" ='3'
    and
    cast ("Id" as varchar) = @id_spr
Qulava commented 10 months ago

In addition. I tried to include the source code projects to my project for catch place where error occure. Error occure in DataConnectionBase class from FastReport.Data when adapter.Fill(table) method call (820 line)

gbellini79 commented 6 months ago

Same problem here with a simple report with 2 text parameters. Have you found any solution or workaround?

Qulava commented 6 months ago

Same problem here with a simple report with 2 text parameters. Have you found any solution or workaround?

Problem was gone after some retry. Probably package updates was help, becouse didnt do any specific movements.

Current Packages

Work example. created in 2023.2.0 version of designer. CatalogLayout.zip

Controller call

[HttpGet("catalog/create/{id}")]
public IActionResult CatalogCreate([Required] Guid id)
{
    var uri = _configuration.GetSection("ImageBaseUri").Value;

    var parameters = new Dictionary<string, string>
    {
        { "company_id", $"{id}" },
        { "image_base_uri", $"{uri}" }
    };

    var filePath = $"Reports/CatalogLayout.frx";

    var pdf = _reportService.MakeReportStream(filePath, parameters);

    return File(pdf, "application/pdf");
}

service example

internal sealed class FastReportService : IReportService
{
    private readonly IConfiguration _configuration;

    public FastReportService(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public Stream MakeReportStream(string fileName, Dictionary<string, string> parameters)
    {
        using var report = new FastReport.Report();

        report.Load(fileName);

        report.Dictionary.Connections[0].ConnectionString = _configuration.GetConnectionString("PostgreSQL");

        foreach (var parameter in parameters)
        {
            report.SetParameterValue(parameter.Key, parameter.Value);
        }

        report.Prepare();

        using var pdfExport = new PDFSimpleExport();

        var stream = new MemoryStream();

        pdfExport.Export(report, stream);

        stream.Position = 0;

        return stream;
    }
}

I hope it helps.

rftd commented 1 month ago

I have the same problem but the with another message.

Writing values of 'FastReport.Variant' is not supported for parameters having NpgsqlDbType 'Bigint'.