none
Can't bind report with the report viewer at runtime RRS feed

  • Question

  • In my application I have one report viewer, but I have multiple reports. I need change the report as per the client input. For example client click for Assets detail. I need to bind the AssetDetail.rdlc report at runtime. I have written a code for that, but it's not working.. I tried the code suggested in internet sources, but that is also not working. I have given my code below.
    private void button1_Click(object sender, EventArgs e)
            {
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT Descriptn, AssetType, Location, Model, SerialNumber, Status, AssetCode, Dateofpurchase, WarrantyExpiry, Condition, Profile FROM Assets", con);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds);
                ReportDataSource reportDataSource = new ReportDataSource();
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "AssestsManagementSystem.ReportAssetList.rdlc";
                this.reportViewer1.LocalReport.ReportPath = "ReportAssetList.rdlc";
                reportDataSource.Value = ds.Tables[0];
                reportDataSource.Name = "DataSet2";
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                this.reportViewer1.Refresh();
                          
            }
    Please help me with this.
    Wednesday, June 3, 2015 6:16 AM

Answers

  • Hi Sarath,

    The below code worked for me, always make sure to use the DataSet name assigned with the report, the one that is created on design time. Also call RefreshReport() instead of Refresh() to load the new report and no need for the ReportPath:

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT Descriptn, AssetType, Location, Model, SerialNumber, Status, AssetCode, Dateofpurchase, WarrantyExpiry, Condition, Profile FROM Assets", con);           
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds);
    this.reportViewer1.LocalReport.DataSources.Clear();
    ReportDataSource reportDataSource = new ReportDataSource();
    reportDataSource.Value = ds.Tables[0];
    reportDataSource.Name = "DataSet2";
    this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
    this.reportViewer1.LocalReport.ReportEmbeddedResource = "AssestsManagementSystem.ReportAssetList.rdlc";
    this.reportViewer1.RefreshReport();



    Fouad Roumieh



    Thursday, June 4, 2015 6:26 PM
  • Then DataSetAssetList in your code instead of DataSet2 for this report.

    Fouad Roumieh

    Friday, June 5, 2015 6:09 AM

All replies

  • Please send error messages if you getting any, you not telling what do you mean by its not working. Please try the below version with .Reset added and let us know:

    private void button1_Click(object sender, EventArgs e)
            {
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT Descriptn, AssetType, Location, Model, SerialNumber, Status, AssetCode, Dateofpurchase, WarrantyExpiry, Condition, Profile FROM Assets", con);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds);
                ReportDataSource reportDataSource = new ReportDataSource();
    	    this.reportViewer1.Reset();
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "AssestsManagementSystem.ReportAssetList.rdlc";
                this.reportViewer1.LocalReport.ReportPath = "ReportAssetList.rdlc";
                reportDataSource.Value = ds.Tables[0];
                reportDataSource.Name = "DataSet2";
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                this.reportViewer1.Refresh();
                          
            }


    Fouad Roumieh

    Wednesday, June 3, 2015 9:59 AM
  • There is no error Message. Report viewer simply showing blank.
    Wednesday, June 3, 2015 10:57 AM
  • The reset didn't work? Are you debugging the code to verify that you DataTable is populated correctly?

    I hope that one of the moderators help us move this to Visual Studio Report Controls for more assistance.


    Fouad Roumieh

    Thursday, June 4, 2015 5:45 AM
  • Yeah I applied Reset code. That is the reason it's showing blank. Otherwise it is showing previous report.
    Thursday, June 4, 2015 6:06 AM
  • Where is the .rdlc file dir?

    Try setting the full path when setting the ReportPath, it shouldn't be only the file name. You can get help here on how to get the path

    Also note that setting the ReportPath will cause the ReportEmbeddedResource property to be ignored. Means either one of the two can be used, so remove the ReportPath.


    Fouad Roumieh

    Thursday, June 4, 2015 6:19 AM
  • Hi Fouad Roumieh, 

    There is no folder for report.

    See the above image (solution Explorer) ReportVendor.rdlc is already binded  with ReportViewer. When I click the button1,  I want bind the ReportAssetList. While loading the report form.

    It  showing ReportVendor.rdlc properly. But while clicking button1 the second report is not binding.



    Thursday, June 4, 2015 6:39 AM
  • Hi Sarath,

    This forum is to discuss data controls such as the DataGridView, and connecting your Windows Forms app to data from a SQL server, a web service or a custom business object, your issue is more related with report viewer, I will move this thread to the more related forum.

    Reference: http://social.msdn.microsoft.com/Forums/en-US/home?forum=vsreportcontrols

    Thanks for your understanding.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, June 4, 2015 7:26 AM
  • Hi Sarath,

    The below code worked for me, always make sure to use the DataSet name assigned with the report, the one that is created on design time. Also call RefreshReport() instead of Refresh() to load the new report and no need for the ReportPath:

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT Descriptn, AssetType, Location, Model, SerialNumber, Status, AssetCode, Dateofpurchase, WarrantyExpiry, Condition, Profile FROM Assets", con);           
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds);
    this.reportViewer1.LocalReport.DataSources.Clear();
    ReportDataSource reportDataSource = new ReportDataSource();
    reportDataSource.Value = ds.Tables[0];
    reportDataSource.Name = "DataSet2";
    this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
    this.reportViewer1.LocalReport.ReportEmbeddedResource = "AssestsManagementSystem.ReportAssetList.rdlc";
    this.reportViewer1.RefreshReport();



    Fouad Roumieh



    Thursday, June 4, 2015 6:26 PM
  • I will try and let you know. Thanks

    Friday, June 5, 2015 5:16 AM
  • I think that above working.. But it showing some error.

    In the above picture it showing  DataSource "DataSetAssetList", But we are using "DataSet2". But why it's showing like that? Actually at the time of designing the Report I chose that DataSet. May be that problem ? I'm completely helpless please help me with this. 

    Friday, June 5, 2015 5:39 AM
  • Right click>Properties on the report file it will show you the DataSet in the Server explorer.

    Also please send a screenshot similar to the one below to check:

    http://stackoverflow.com/questions/21539795/a-data-source-instance-has-not-been-supplied-for-the-data-source-dataset1


    Fouad Roumieh

    Friday, June 5, 2015 5:47 AM
  • I actually try to find report properties there is no DataSet Name. But I selected the Tablix, that time I found the DataSet Name. I Attached both screenshot for you reference.
    Friday, June 5, 2015 6:05 AM
  • Then DataSetAssetList in your code instead of DataSet2 for this report.

    Fouad Roumieh

    Friday, June 5, 2015 6:09 AM
  • WoW It's working!! Thanks Lot.! I have another problem related to ReportViewer Only. Can you help me?
    Friday, June 5, 2015 6:16 AM
  • WoW It's working!! Thanks Lot.! I have another problem related to ReportViewer Only. Can you help me?

    Cool, glad for that :). Please post another question in this forum we need to keep each post for one specific case so other users can benefit form. I will try to help if I have answer, otherwise other experts will do.

    Thanks for your understanding.


    Fouad Roumieh

    Friday, June 5, 2015 6:23 AM
  • Thanks for your help and effort. I thinks everything going smoothly. If I face any problem I will let you know! Thanks again!
    Friday, June 5, 2015 6:31 AM