Core 6.0 RDLC Report

jewel 841 Reputation points
ASP.NET Core 6.0 How to add data from database to RDLC report? My method of adding data without database is working fine. But I can't find any way to add database data. I would appreciate it if someone could help. thanks in advance

public IActionResult Print() 
{ 	var dt =new DataTable(); 	
dt = Getcompanylist(); 	int extension = 1;  	
String mimeType = "";  
var path = ${this._webHostEnvironment.WebRootPath}\\Reports\\Report1.rdlc"; 	
Dictionary<String,string> parmeter = new Dictionary<String,string>(); 	
parmeter.Add("prm", "rdlc repirt");  	
LocalReport localreport = new LocalReport(path);  	localreport.AddDataSource("dscompany", dt); 	
var restul = localreport.Execute(RenderType.Pdf, extension, parmeter, mimeType);   	
return File(restul.MainStream, "application/pdf"); }  

//This method works.
public DataTable Getcompanylist() 
{var dt = new DataTable(); 	
dt.Columns.Add("CompanyName"); 	dt.Columns.Add("CompanyContuct"); 	
DataRow row; 	
for (int i = 0;i<=5; i++) 	
{ row =  dt.NewRow(); 		
row["CompanyName"] = i; 		
row["CompanyContuct"] = "Mr jewe"+i; 		
row["Email"] = ""; 		
 return dt; }

//This is my database data. I want to get data from here.
public IActionResult Getcompanylist() 
{ 	var compnay = _context.tbl_Compnays.ToList(); 	
return View(); }
A set of technologies in the .NET Framework for building web applications and XML web services.
4,383 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,811 Reputation points Microsoft Vendor

    Hi @jewel,

    You should convert the List<object> to DataTable. Then the issue will be fixed.

    Here is the test code:

            public DataTable Getcompanylist()
                var compnay = _context.tbl_Compnays.ToList();
                DataTable dt = ToDataTableByList(compnay);
                return dt;
            private static DataTable ToDataTableByList<T>(List<T> list)
                List<PropertyInfo> pList = new List<PropertyInfo>();
                Type type = typeof(T);
                DataTable dt = new DataTable();   
                Array.ForEach<PropertyInfo>(type.GetProperties(), p =>
                    dt.Columns.Add(p.Name, p.PropertyType);
                if (list != null && list.Count > 0)
                    foreach (var item in list)
                        DataRow row = dt.NewRow();
                        pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                return dt;

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,


    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful