How to generate RDLC report using loop?

jewel 1,231 Reputation points
2025-02-06T16:01:01.2833333+00:00

I asked a question about this earlier here. But not getting the correct solution, I again asked for the help of experienced people.

I have shown a sample of my data below. I want to print this data on separate pages according to empid. I guess it can be done using a loop.

I have also attached an image to make it easier to understand.

  public IActionResult testPrint(int Ids)
  {
      List<tbl_order> orders = new List<tbl_order>()
      {
          // empid=1 and Customerid=1 Data
         new tbl_order{Id = 1,empid=1 ,Date = DateTime.Parse("2025,2,1"),productid=1,customerid=1,Qty=2,value=100},
         new tbl_order{Id = 2,empid=1 , Date = DateTime.Parse("2025,2,1"),produScreenshot (36).pngctid=2,customerid=1,Qty=1,value=200},
         
         // empid=1 and Customerid=2 Data
         new tbl_order{Id = 3,empid=1 , Date = DateTime.Parse("2025,2,1"),productid=3,customerid=2,Qty=3,value=1500},
         new tbl_order{Id = 4,empid=1 , Date = DateTime.Parse("2025,2,1"),productid=1,customerid=2,Qty=1,value=50},
         
         // empid=1 and Customerid=3 Data
         new tbl_order{Id = 5,empid=1 , Date = DateTime.Parse("2025,2,1"),productid=2,customerid=3,Qty=2,value=400},
            
         // empid=1 and Customerid=4 Data
         new tbl_order{Id = 6,empid=1 , Date = DateTime.Parse("2025,2,1"),productid=3,customerid=4,Qty=4,value=2000},
         new tbl_order{Id = 7, empid=1 ,Date = DateTime.Parse("2025,2,1"),productid=4,customerid=4,Qty=2,value=3000},
         new tbl_order{Id = 8,empid=2 ,Date = DateTime.Parse("2025,2,1"),productid=1,customerid=5,Qty=4,value=200},
         new tbl_order{Id = 9,empid=2 , Date = DateTime.Parse("2025,2,1"),productid=2,customerid=5,Qty=6,value=1200},
         new tbl_order{Id = 10,empid=2 , Date = DateTime.Parse("2025,2,1"),productid=3,customerid=6,Qty=1,value=500},
      };
  var Printdata = (from a in orders.Where(x => x.empid == 1)

                   group a by a.customerid into g

                   select new

                   {

                       customerid = g.Key,

                       productid = g.Select(x => x.productid),

                       Qty = g.Select(x => x.Qty),

                       value = g.Select(x => x.value),

                   }).ToList();



  var parameterdata = orders.Where(x => x.empid == 1).FirstOrDefault().customerid;

  String customerid=parameterdata.ToString();

  int extension = (int)(DateTime.Now.Ticks >> 10);

  String path = _webHostEnvironment.WebRootPath + @"\Reports\xyz.rdlc";

  Dictionary<String, string> parmeter = new Dictionary<String, string>();

  parmeter.Add("customer", customerid);

      LocalReport localreport = new LocalReport(path);

      localreport.AddDataSource("DataSet1", Printdata);

      var report = localreport.Execute(RenderType.Pdf, extension, parmeter, "");

  return File(report.MainStream, "application/pdf");

}

  public class tbl_order
  {
      public int Id { get; set; }
      public DateTime Date { get; set; }
      public int empid { get; set; }
      public int Qty { get; set; }
      public int productid { get; set; }
      public Decimal value { get; set; }
      public int customerid { get; set; }
  }

Screenshot (36)

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Raymond Huynh (WICLOUD CORPORATION) 620 Reputation points Microsoft External Staff
    2025-07-08T08:04:36.9766667+00:00

    If you want each employee’s data to appear on a separate page in your RDLC report, you don’t actually need to generate multiple reports in a loop. Instead, you can use grouping and page breaks in your RDLC design, and just pass all your data at once. Here’s how you can do it:

    1. Group Your Data in the RDLC Report

    • In the RDLC designer, add a table or list.
    • Right-click the row handle (on the left) and choose “Add Group” → “Parent Group…”.
    • Set the group expression to =Fields!empid.Value.
    • In the group properties, go to the Page Breaks section and check Between each instance of a group. This will force a new page for each empid.

    2. Pass All Data to the Report

    • You don’t need to filter by empid in your C# code. Just pass the entire list of orders to the report’s datasource.

    3. Sample C# Code

    public IActionResult testPrint()
    {
        List<tbl_order> orders = new List<tbl_order>()
        {
            // ... your sample data ...
        };
     
        // No need to filter or group in C#; let RDLC handle it
        string path = _webHostEnvironment.WebRootPath + @"\Reports\xyz.rdlc";
        LocalReport localreport = new LocalReport(path);
        localreport.AddDataSource("DataSet1", orders);
     
        // If you want to pass parameters, you can, but not needed for grouping
        var report = localreport.Execute(RenderType.Pdf, 1, null, "");
        return File(report.MainStream, "application/pdf");
    }
    

    4. Result

    • When you run the report, each employee’s data will start on a new page, thanks to the group and page break settings in the RDLC file.

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.