I am trying to get a subreport working. I am using VS 2010. I've inserted a "Subreport1" into my main report and keep getting the error: Data retrieval failed for the subreport, 'Subreport1', located at….
Here's my code:
public partial class rptEngOrd : Form
{
public rptEngOrd()
{
InitializeComponent();
}
private List<EngOrd> eoNumParam;
private string eonum;
private DataSet dsReport;
private DataSet dsSubReport;
private void rptEngOrd_Load(object sender, EventArgs e)
{
LoadComboBoxes();
}
private void LoadComboBoxes()
{
try
{
eoNumParam = EngOrdDB.GetDistinctEONum();
cboEONum.DataSource = eoNumParam;
cboEONum.SelectedIndex = -1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}
private void btnPreview_Click(object sender, EventArgs e)
{
try
{
if (cboEONum.SelectedValue.ToString().Equals("zALL"))
{
eonum = "";
}
else
{
eonum = cboEONum.SelectedValue.ToString();
}
//get the data
dsReport = EngOrdDB.GetEngOrdbyEONum(eonum);
//provide local report information to viewer
reportViewer1.LocalReport.ReportEmbeddedResource = "CustomMenu.rptEngOrd.rdlc";
reportViewer1.ProcessingMode = ProcessingMode.Local;
//prepare report data source
ReportDataSource rds = new ReportDataSource();
rds.Name = "dsReport";
rds.Value = dsReport.Tables[0];
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(rds);
//prepare sub report data source
dsSubReport = EngOrdTaskDB.GetEngOrdTaskbyEONum(eonum);
ReportDataSource rdsSub = new ReportDataSource();
rdsSub.Name = "dsSubReport";
rdsSub.Value = dsSubReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rdsSub);
reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(srptProcessingEventHandler);
//load the report viewer
reportViewer1.LocalReport.Refresh();
//MessageBox.Show(eonum);
}
catch (Exception ex)
{
//display generic message back to user
MessageBox.Show(ex.Message);
}
finally
{
}
reportViewer1.RefreshReport();
}
private void srptProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
ReportDataSource r = reportViewer1.LocalReport.DataSources[1];
e.DataSources.Add(r);
}
}
Best Answer
I found a while a go this link that help me with the sub report data population, Reporting against a domain model, you find the code that interests you about at the middle of the page.
By the way I think that you should populate and add the data only in the event handler
and you delete this code from the click event.
In this way i should work. Hope this helps