Home > Blockchain >  How to return data from the database query?
How to return data from the database query?

Time:12-23

I'm trying to Implement ExportToPDF() action method in my MCV.CORE web app. When action method is called ExportToPDF() it should export all data from the database. But only Heder row is shown. And data form database is not shown at all.

I'm not sure how to correctly implement this action method, and what I'm doing wrong here?

My simplified code:

public class EmployeeCategory
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string? Name { get; set; }

    [Required]
    public string? LastName { get; set; }

    [Required]
    public string? Address { get; set; }

    [Required]
    public double NetSalary { get; set; }
    
    [Required]
    public double GrossSalary { get; set; }

}

public class EmployeeCategoryController : Controller
{

    private ApplicationDbContext Context { get; }
 
    public EmployeeCategoryController(ApplicationDbContext _context)
    {
        Context = _context;
    }
 
    public IActionResult Index()
    {
        return View(this.Context.EmployeeCategories.Take(6).ToList());
    }


        [HttpPost]
        public FileResult ExportToPDF()
        {

            var employees = new List<Employee>();

            
            var query = employees.AsQueryable().Take(6).Select(employee => new
            {
        employee.Id, employee.Name, employee.LastName, employee.Address, employee.GrossSalary, employee.NetSalary
         }).ToList();
        


            

            //Building an HTML string.
            StringBuilder sb = new StringBuilder();

            //Table start.
            sb.Append("<table border='1' cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-family: Arial; font-size: 10pt;'>");

            //Building the Header row.
            sb.Append("<tr>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Id</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Name</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>LastName</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Address</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>NetSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>GrossSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>NetSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>GrossSalary</th>");
            sb.Append("</tr>");


            //Building the Data rows.
            query.ForEach(employee =>
            {
            sb.Append("<tr>");
            foreach (var propertyInfo in employee.GetType().GetProperties())
            {
            sb.Append("<td style='border: 1px solid #ccc'>");
            sb.Append(propertyInfo.GetValue(employee));
            sb.Append("</td>");
            }
            sb.Append("</tr>");
            });





            //Table end.
            sb.Append("</table>");

            using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString())))
            {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                PdfWriter writer = new PdfWriter(byteArrayOutputStream);
                PdfDocument pdfDocument = new PdfDocument(writer);
                pdfDocument.SetDefaultPageSize(PageSize.A4);
                HtmlConverter.ConvertToPdf(stream, pdfDocument);
                pdfDocument.Close();
                return File(byteArrayOutputStream.ToArray(), "application/pdf", "EmployeeList.pdf");
            }
        }
}

CodePudding user response:

Error I found here:

 var employees = new List<Employee>();

 var query = employees.AsQueryable().Take(6).Select(employee => new
             {
                 employee.Id, employee.Name, employee.LastName, employee.Address, employee.GrossSalary, employee.NetSalary
             }).ToList();

You should change code to

Context.Employees.Take(6).Select(m => new Employee{ Id =m.Id, 
                        Name=m.Name
                        ...
                        }).ToList();

CodePudding user response:

Refactored Code:

public class EmployeeCategory
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string? Name { get; set; }

    [Required]
    public string? LastName { get; set; }

    [Required]
    public string? Address { get; set; }

    [Required]
    public double NetSalary { get; set; }
    
    [Required]
    public double GrossSalary { get; set; }

}

public class EmployeeCategoryController : Controller
{

    private ApplicationDbContext Context { get; }
 
    public EmployeeCategoryController(ApplicationDbContext _context)
    {
        Context = _context;
    }
 
    public IActionResult Index()
    {
        return View(this.Context.EmployeeCategories.Take(6).ToList());
    }


        [HttpPost]
        public FileResult ExportToPDF()
        {

            

            // Fixed query
            var query = Context.EmployeeCategories.Take(6).Select(m => new EmployeeCategory
            {
              Id = m.Id,
              Name = m.Name,
              LastName = m.LastName,
              Address = m.Address,
              NetSalary = m.NetSalary,
              GrossSalary = m.GrossSalary,
            }).ToList();
        


            

            //Building an HTML string.
            StringBuilder sb = new StringBuilder();

            //Table start.
            sb.Append("<table border='1' cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-family: Arial; font-size: 10pt;'>");

            //Building the Header row.
            sb.Append("<tr>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Id</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Name</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>LastName</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Address</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>NetSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>GrossSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>NetSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>GrossSalary</th>");
            sb.Append("</tr>");


            //Building the Data rows.
            query.ForEach(employee =>
            {
            sb.Append("<tr>");
            foreach (var propertyInfo in employee.GetType().GetProperties())
            {
            sb.Append("<td style='border: 1px solid #ccc'>");
            sb.Append(propertyInfo.GetValue(employee));
            sb.Append("</td>");
            }
            sb.Append("</tr>");
            });





            //Table end.
            sb.Append("</table>");

            using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString())))
            {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                PdfWriter writer = new PdfWriter(byteArrayOutputStream);
                PdfDocument pdfDocument = new PdfDocument(writer);
                pdfDocument.SetDefaultPageSize(PageSize.A4);
                HtmlConverter.ConvertToPdf(stream, pdfDocument);
                pdfDocument.Close();
                return File(byteArrayOutputStream.ToArray(), "application/pdf", "EmployeeList.pdf");
            }
        }
}
  • Related