1

I am trying to map a DataTable to a dto DataTable but facing the error. I am using CQRS and MediatR. Here is my code.

I gave a basic implementation here.

Controller.cs

 [Route("api/[controller]")]
[ApiController]
public class MapDataTableDemo : ControllerBase
{
    TempRepo _tempRepo;
    private readonly IMediator _mediator;
    public MapDataTableDemo(IMediator mediator)
    {
        _tempRepo = new TempRepo();
        _tempRepo.AddDataToDataTable();
        _mediator = mediator;
    }

    [HttpGet]
    public async Task<IActionResult> MapDataTable()
    {
        var response = _mediator.Send(new GetDataTableQuery());
        return  Ok(response);
    }
}

GetDataTableQuery.cs

 public record GetDataTableQuery:IRequest<ResponseDto>
{
}

GetDataTableHandler.cs

    using MediatR;
using MapDataTable.Queries;
using System.Data;
using MapsterMapper;
namespace MapDataTable.Handlers
{
    public class GetTableHandler : IRequestHandler<GetDataTableQuery, ResponseDto>
    {
        TempRepo _tempRepo;
        private readonly IMapper _mapper;
        public GetTableHandler(IMapper mapper)
        {
            _tempRepo = new TempRepo();
            _tempRepo.AddDataToDataTable();
            _mapper = mapper;
        }
        public async Task<ResponseDto> Handle(GetDataTableQuery request,
                                                CancellationToken cancellationToken)
        {
            var response = _tempRepo.GetDataTable();
            return  new ResponseDto()
            {
                dataTable = _mapper.Map<DataTable>(response)// here i am trying to map datatables
            };
        }
    }
}

ResponseDto.cs

  public class ResponseDto
{
    public DataTable dataTable { get; set; }
}

TempRepo.cs

  using System.Data;

namespace MapDataTable
{
    public class TempRepo
    {
        public DataTable dataTable;
        public TempRepo()
        {
            dataTable = new DataTable();
        }
        public void AddDataToDataTable()
        {
            dataTable.Columns.Add("Id");
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Price");
            dataTable.Rows.Add(1,"shoes",3000);
            dataTable.Rows.Add(2, "mobile", 40000);
            dataTable.Rows.Add(3, "watch", 5000);
        }
        public DataTable GetDataTable()
        {
            return dataTable;
        }
    }
}

It gives the following error.

Undocumented

Error: response status is 500

Response body Download

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Result' on 'System.Threading.Tasks.Task`1[MapDataTable.ResponseDto]'.
 ---> System.AggregateException: One or more errors occurred. (Error while compiling
source=System.Data.DataTable
destination=System.Data.DataTable
type=Map)
 ---> Mapster.CompileException: Error while compiling
source=System.Data.DataTable
destination=System.Data.DataTable
type=Map
 ---> Mapster.CompileException: Error while compiling
source=System.Globalization.CultureInfo
destination=System.Globalization.CultureInfo
type=Map
 ---> System.InvalidOperationException: No default constructor for type 'CultureInfo', please use 'ConstructUsing' or 'MapWith'
   at Mapster.Adapters.BaseAdapter.CreateInstantiationExpression(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.ClassAdapter.CreateInstantiationExpression(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.ClassAdapter.CreateInlineExpression(Expression source, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateInlineExpressionBody(Expression source, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   --- End of inner exception stack trace ---
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateInlineMapExpression(Type sourceType, Type destination

How do I map them?

Aman
  • 13
  • 3
  • I don't see why you need to map to a `DataTable` object from a `DataTable` object. While I doubt that you may have the serialization problem that you are returning a response with `DataTable` data. Instead, try to return the response with an array which is serializable. – Yong Shun Oct 13 '22 at 07:09

0 Answers0