0

I created report viewer in ASP.NET MVC, between 2 tables.

I created adapter to get all data in report like this :

enter image description here

But when I export report come like this, the expence_type does not show the data:

enter image description here

This is my code:

 public ActionResult Reports(string ReportType)
    {
        LocalReport localreport = new LocalReport();
        localreport.ReportPath = Server.MapPath("~/Reports/ExpenseReport.rdlc");



        ReportDataSource reportDataSource = new ReportDataSource();

        reportDataSource.Name = "DataSetE";

        var dataList = (from ed in _context.ExpenseDetails
         join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id

     select new { id = ed.ExpenseDetails_Id, amount = ed.Amount, expense = ed.DateExpense, eid = ed.ExpensesId, expenseType = e.Expenses_Type }).ToList();

        reportDataSource.Value = dataList;


        reportDataSource.Value = _context.ExpenseDetails.ToList();

        localreport.DataSources.Add(reportDataSource);

        string reportType = ReportType;
        string mimeType;
        string encoding;
        string fileNameExtension;

        if (reportType == "Excel")
        {
            fileNameExtension = "xlsx";
        }

        if (reportType == "Word")
        {
            fileNameExtension = "docx";
        }

        if (reportType == "PDF")
        {
            fileNameExtension = "pdf";
        }

        else
        {
            fileNameExtension = "jpg";

        }

        string[] streams;
        Warning[] warnings;
        byte[] renderedByte;

        renderedByte = localreport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

        Response.AddHeader("content-disposition", "attachment;filename = expens_report." + fileNameExtension);

        return File(renderedByte, fileNameExtension);
    }

Expense Details Models :

public class ExpenseDetails
{
    [Key]
    public int ExpenseDetails_Id { get; set; }

    public double Amount { get; set; }

    public DateTime DateExpense { get; set; }

    public int ExpensesId { get; set; }

    [ForeignKey("ExpensesId")]
    public virtual Expenses expenses { get; set; }
}

Expenses Models :

   public class Expenses
{
    [Key]
    public int Expenses_Id { get; set; }
    public string Expenses_Type { get; set; }
}

How can I show the Expence_Type ?

reportDataSource.Value = _context.ExpenseDetails.ToList();

I tried to add the other table in this code but it is not returned.

enter image description here

enter image description here

Barbora
  • 921
  • 1
  • 6
  • 11
programmer
  • 49
  • 10

1 Answers1

0

You can create join between two table and assign this as your report data source

Try this

var dataList = (from ed in _context.ExpenseDetails
                 join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id 
                 select new {
                     id = ed.ExpenseDetails_Id ,
                     amount = ed.Amount ,
                     expense = ed.DateExpense ,
                     eid = ed.ExpensesId ,
                     expenseType = e.Expenses_Type 
                 }).ToList();

and add this

reportDataSource.Value = dataList ; 
Mustufa
  • 116
  • 4
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/191652/discussion-on-answer-by-mustufa-join-2-tables-in-report-viewer-in-asp-net-mvc). – Samuel Liew Apr 11 '19 at 11:38