简体   繁体   中英

Use more than one Datatable in Dataset

I have to create a crystal report , whose data will be populated from Dataset. Dataset has three datatable. Namely:

CustDetais
BookingDetails
FoodNExtra

When I setup crystal report through Crystal Report Wizard , I got new screen which says 在此处输入图片说明 Not sure what to do here so I just clicked next. And heres my code for crystal report viewer :

private void CRKOTQoute_Load(object sender, EventArgs e)
{
    try
    {
        MySqlCommand cmd = new MySqlCommand("select CustName,Phone,Address,Email from tblCustDetails where custid=@custid", con.con);
        cmd.Parameters.AddWithValue("@custid", BLDashboard.custid);
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
        DataSet1 ds = new DataSet1();
        adapter.Fill(ds, "CustDetais");
        if (ds.Tables["CustDetais"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        MySqlCommand cmd1 = new MySqlCommand("select BookingID,BookingDate,Event,EventDate,EventTime,Pax,Service,ServiceTime from tblBookingDetails where BookingID=@Bookid", con.con);
        cmd.Parameters.AddWithValue("@bookid", BLDashboard.bookingID);
        MySqlDataAdapter adapter1 = new MySqlDataAdapter(cmd1);
        DataSet1 ds1 = new DataSet1();
        adapter.Fill(ds1, "BookingDetails");
        if (ds1.Tables["BookingDetails"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        MySqlCommand cmd2 = new MySqlCommand("select FoodMenu,ExtraItem from tblItem where BookingID=@Bookid1", con.con);
        cmd.Parameters.AddWithValue("@bookid1", BLDashboard.bookingID);
        MySqlDataAdapter adapter2 = new MySqlDataAdapter(cmd2);
        DataSet1 ds2 = new DataSet1();
        adapter.Fill(ds2, "BookingDetails");
        if (ds2.Tables["FoodNExtra"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        RPTKOTQoute printKOTqoute = new RPTKOTQoute();
        //RPTKitchenQoute printKOTqoute = new RPTKitchenQoute();
        //RPTKOTTest printKOTqoute = new RPTKOTTest();
        printKOTqoute.SetDataSource(ds);
        printKOTqoute.SetDataSource(ds1);
        printKOTqoute.SetDataSource(ds2);
        crystalReportViewer1.ReportSource = printKOTqoute;
        System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
        printKOTqoute.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;
        printKOTqoute.PrintOptions.PrinterName = "EPSON TM-U220 Receipt";
        printKOTqoute.PrintToPrinter(1, false, 0, 0);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

I used same way as mentioned here .

And when I run crystal report theres no error but no data is displayed. I tried using just One datatable and it worked fine. Also I am using MySQL as database.

Try and merge the three datasets into one before assigning them to the report data source:

ds.Merge(ds1)
ds.Merge(ds2)

This way, ds should include all the data (all tables) in ds , ds1 and ds2 . Then, assign only ds as the DataSource of the report.

I think that Crystal Reports supports only one SELECT statement as data source per report, (at least this is the case in Crystal Reports 8.5 UI which I have used), if you use more its behaviour is a bit unpredictable. That is probably why the wizard asks from you to join the tables. If a join query that would bring all the data wanted is not a solution for you, then probably the only solution would be to add subreports in your report. But still you cannot add them programmatically, you should add them in design mode, and then attach the datasets as datasources via code like this:

printKOTqoute.Subreports[0].SetDataSource(ds);
printKOTqoute.Subreports[1].SetDataSource(ds1);
printKOTqoute.Subreports[2].SetDataSource(ds2);

Also check this out How to set datasource of Sub crystal report in c# win form app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM