none
Assign Local Reports to ReportViewer dynamically RRS feed

  • Question

  • I'm trying to assign datasource to reportviewer dynamically. Actually, I need this cuz I dont want to create separate form for each report. I've gone through some of the articles written on this topic but somehow my problem still exists.

    I have a folder named Reports in my project wich contains Report1.rdlc. I dragged and dropped a reportviewer onto the form named reportViewer1. I did'nt used the smart tag select the report at design time. Instead I used a buton to do the work programatically as follows:

    Code Block

    private void button1_Click(object sender, EventArgs e)

            {
                reportViewer1.Reset();
                CustomerTableAdapter.Fill(DatabaseDataSet.Customer);
                ReportDataSource ds = new ReportDataSource("DatabaseDataSet_Customer",CustomerTableAdapter.GetData());
                ds.Value = CustomerBindingSource;
                reportViewer1.LocalReport.DataSources.Clear();
                reportViewer1.LocalReport.DataSources.Add(ds);
                reportViewer1.LocalReport.ReportEmbeddedResource = "TestApp.Reports.Report1.rdlc";
                reportViewer1.Refresh();
            }


    Initially the reportviewer displays a grayed message "the source of the report definition has not been defined", which is quite obvious and expected as its not bound to any report source yet. When the button is clicked the message disappears, but nothing gets displayed.

    To verify that datasource gets filled correctly, I dragged and dropped another reportviewer onto the same form and this time used the smart tag to select "Report1.rdlc". When executed the application the second reportviewer displays the data correctly.

    In debug mode I applied watch on both reportviewers' property "LocalReport.Datasources"; Checked the name and value of the 0th datasource, which came out same. Other porperties of LocalReport also matched like "ReportEmbeddedResource", "ReportPath".

    I really don't know how resolve this issue. Plz help me workout this problem.

    Thanks you in advance.


    Monday, December 3, 2007 7:22 AM

All replies

  • I could be wrong, but shouldn't you assign the .rdlc template before the datasources?

     

    So

     

    reportViewer1.LocalReport.ReportEmbeddedResource = "TestApp.Reports.Report1.rdlc";

    reportViewer1.LocalReport.DataSources.Clear();

     

    ReportDataSource ds = new ReportDataSource();

    blah blah blah

    yakkity yakkity

     

    ReportViewer1.LocalReport.DataSources.Add(ds);

    ReportViewer1.LocalReport.Refresh();

    Monday, December 3, 2007 6:02 PM
  • Nope, he has the standard order. It's pretty common to swap out data sources on a loaded report definition, FWIW; you might do this if you were allowing the user to set filters, for example.

     

    Here's my walkthrough, you can see that he's using the same order: http://spacefold.com/lisa/post/Dynamically-loading-reports-in-ReportViewers.aspx

     

    >L<

    Tuesday, December 4, 2007 3:43 PM
  • Hi there,

     

    Are you sure the embedded resource is really available?  Is this an ASP.NET application, and how did you set up your resources?  You might want to try not embedding as a test (deliver the RDLC as a separate file) and see if that works.

     

    >L<

     

    Tuesday, December 4, 2007 3:46 PM
  •  

    ahh cool, well good to know Lisa.  I'm actually loading my dynamically, but I'm using the load report definition method, ,and it's on a webpage...so they are slighty different.  But of course you know that heh.
    Tuesday, December 4, 2007 3:49 PM
  • Both sequences will work and if you're loading yours using the load report definition method, then there is a good chance you are building the RDLC on the fly, yes?  In which case there would probably be no reason to swap out the datasources out from "under" the RDLC, unlike the filter example I gave. 

     

    So, sure, it makes sense in your situation: you think of the datasources as the "stable" element, you load those first, and then you swap out the "unstable" report elements out from underneath those datasources, as needed, per user requests.  (You might even be doing filtering on the table level, within the report.)

     

    This is all for the benefit of anybody reading who might otherwise get confused: there are *critical* parts of the sequence (like when you do the .Reset in relation to the load stuff) and other parts that are not so critical.

     

    Meanwhile... no, it's really not different on a web page. (I showed the winform control in that walkthrough, but it's pretty much the same in both -- I hope I made that clear somewhere in the text.)  However, people do have troubles deploying reports as embedded resources to ASP.NET applications, which is why I asked the question I did.  (In your situation, this isn't an issue -- no resource to embed *or* deploy if you are loading the report def as a stream <s>.)

     

    >L<

    Tuesday, December 4, 2007 4:25 PM
  • great info thanks, yeah, in my situation all the .RDLC files are stored on a seperate file server as byte arrays, and so are the datasets for the reports, so at runtime I have to get a dataset, figure out what .RDLC file it needs and then go get the correct one from our file server....hence I use the load report definition because there is no file path to reference. 

     

    sorry to the original poster for the thread Hijacking. 

    Tuesday, December 4, 2007 5:15 PM
  • try

     

     

                    ReportViewer1.LocalReport.ReportPath = "BodyshopRanking.rdlc";

                    ReportViewer1.LocalReport.Refresh();

    rather then
       reportViewer1.LocalReport.ReportEmbeddedResource = "TestApp.Reports.Report1.rdlc";
                reportViewer1.Refresh();
    same worked for me

    Friday, May 13, 2011 2:36 AM