Populate Crystal Report using dataset with multiple tables using C# in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hello,

I am trying to populate crysatl report using dataset. I tried the reference from the link. https://www.aspsnippets.com/Articles/Crystal-Report-ASPNet-Example-using-DataSet-or-DataTable-in-C-VBNet-and-Visual-Studio-2010.aspx. But now the issue for me is that i need to show fields of two tables in the CR.

1) I tried combing the two select statements and fill the data, but failed.

2) I cannt use direct database table, as this give me Database Login Issue.

3) I cann't join the two select statement, because the tables dont have a joinning(common field)

Please help me develop a code, through which i can populate data in my crytal report. The report will have fields from two tables.

 

Regards

Posted 2 months ago

Hi delhigle,

For displaying data from multiple table you need to do the following steps.

1. You need to add another datatable to the dataset(.xsd file)

2. Then in Field Explorer Right click on Database Fields -> Database Export.. -> Right click on DataSetname Refresh it.

3. Then add the new table to the Selected Tables section and click ok.

4. Now add the fields to the report as per your requirement.

5. Change the code with the below to display data from multiple table.

C#

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = GetData("select * from customers GO select EmployeeID,FirstName,City,Country from Employees");
    //crystalReport.SetDataSource(dsCustomers);
    crystalReport.Database.Tables[0].SetDataSource(dsCustomers.Tables["Table"]);
    crystalReport.Database.Tables[1].SetDataSource(dsCustomers.Tables["Table1"]);
    CrystalReportViewer1.ReportSource = crystalReport;
}

private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;

            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers);
                return dsCustomers;
            }
        }
    }
}

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html