1
votes

My ASP C# application has one Crystal report with a main report and one subreport that is within the main report itself. My main report lists students and the subreport is expected to list the mark list for students those are listed. I am populating the datatable for the main report while a button is clicked, that works fine. However, do not know how to link main and sub reports. I have defined one parameter that links both the datasets, however I don't have any idea how to populate the 2nd datatable when the button is clicked. Would appreciate some samples, which I couldn't find after searching hours (mostly because I don't know the correct terms to search with)

1

1 Answers

0
votes

By combining these threads, I've devised a solution

  1. https://www.c-sharpcorner.com/article/generate-Asp-Net-crystal-report-using-dataset/
  2. How to set datasource of Sub crystal report in c# win form app

All I needed was to populate the datatables within the dataset & to link the subreport(s) on common columns. Sample code provided below.

using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace insert_demo
{
    public partial class MarkList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //DataTable dt = new DataTable();

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|insert_demo.mdf;Integrated Security=True");

            SqlDataAdapter da = new SqlDataAdapter(@"SELECT * from tbl_data", con);        
            Students ds = new Students();
            da.Fill(ds, "StudentTable");


            SqlDataAdapter da1 = new SqlDataAdapter(@"SELECT * from STUDENT_SCORES", con);
            Students ds1 = new Students();
            da1.Fill(ds1, "MarksTable");



            ReportDocument oRpt = new ReportDocument();
            oRpt.Load(Server.MapPath(@"~/CrystalReport4.rpt"));
            oRpt.SetDataSource(ds);

            oRpt.Subreports[0].DataSourceConnections.Clear();
            oRpt.Subreports[0].SetDataSource(ds1);



            CrystalReportViewer1.Visible = true;
            CrystalReportViewer1.ReportSource = oRpt;


        }
    }
}