asp.net Core 6.0 RDLC Report

jewel 1,186 Reputation points
2023-12-02T16:10:42.6+00:00
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"); 	
dt.Columns.Add("Email");  	
DataRow row; 	
for (int i = 0;i<=5; i++) 	
{ row =  dt.NewRow(); 		
row["CompanyName"] = i; 		
row["CompanyContuct"] = "Mr jewe"+i; 		
row["Email"] = "******@gmail.com"; 		
dt.Rows.Add(row);	 		 	
}    
 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(); }
Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Anonymous
    2023-12-04T03:30:10.89+00:00

    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 =>
                {
                    pList.Add(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));
                        dt.Rows.Add(row);
                    }
                }
                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,

    Jason

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.