Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set datasource of Sub crystal report in c# win form app

I am using this code to load a main report and a subreport inside the main report. The main report is a blank one and just contains subreport.

Here is my code:

MySqlConnection cnn;
string connectionString = null;
string sql = null;

connectionString = "Server = BC; Database = mydb1; Uid = root; Pwd = abc123;";
cnn = new MySqlConnection(connectionString);
cnn.Open();

sql = "SELECT * from mytable1 ";
MySqlDataAdapter dscmd = new MySqlDataAdapter(sql, cnn);
DataSet1 ds = new DataSet1();
dscmd.Fill(ds, "Imagetest");
cnn.Close();

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("C:/Subreport.rpt");
cryRpt.SetDataSource(ds.Tables[1]);

crystalReportViewer1.ReportSource = "C:/MainReport.rpt";
crystalReportViewer1.Refresh();

When I run the application I just see main report with blank subreport.

like image 773
h_a86 Avatar asked Nov 04 '11 07:11

h_a86


1 Answers

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("C:/MainReport.rpt");
cryRpt.DataSourceConnections.Clear();
cryRpt.SetDataSource(ds.Tables[0]);
cryRpt.Subreports[0].DataSourceConnections.Clear();
cryRpt.Subreports[0].SetDataSource(ds.Tables[0]);
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
like image 138
Justin Avatar answered Nov 09 '22 10:11

Justin