locked
Assigning ReportViewer DataSources on the fly RRS feed

  • Question

  • User-1846718393 posted

     I am using a single ReportViewer control to display several reports on a single page.  Depending on selections by the user, the Report (RDLC) path, and datasource is selected.

    I've designed 2 reports:  Report1.rdlc and Report2.rdlc
    And 2 DataSets:  DataSet1 with Table DataSet1, and DataSet2 with Table DataSet2

    In the button1_click event, i've got the code behind:

            ReportViewer1.ProcessingMode = ProcessingMode.Local
            Dim rep As LocalReport = ReportViewer1.LocalReport
            rep.ReportPath = "Report1.rdlc"

            Dim printRep As New MyClass
            Dim dsRep As New ReportDataSource
            dsRep.Name = "DataSet1_DataSet1"
            dsRep.Value = printRep.GetDataTable("Select * from Whatever", "Whatever")        ' This function simply returns a DataTable
            rep.DataSources.Clear()
            rep.DataSources.Add(dsInvoice)

    In another button2_click event, the code is almost identical, except for the Report Path, DataSource name and Sql Statement to get the DataSet:

            ReportViewer1.ProcessingMode = ProcessingMode.Local
            Dim rep As LocalReport = ReportViewer1.LocalReport
            rep.ReportPath = "Report2.rdlc"

            Dim printRep As New MyClass
            Dim dsRep As New ReportDataSource
            dsRep.Name = "DataSet2_DataSet2"
            dsRep.Value = printRep.GetDataTable("Select * from SomethingElse", "SomethingElse")        --This function simply returns a DataTable
            rep.DataSources.Clear()
            rep.DataSources.Add(dsInvoice)

    The problem I am having is the first time this is run, it works correctly.  However, if I click on the next button, i receive the error that a datasource instance has not been specified:

    1)  Click button1 - The report displays correctly

    2) Click button2 - Receive error:  A DataSource Instance has not been supplied for DataSource "DataSet1_DataSet1".

    If i close and re-start the page, and start with button2, then click button1, button2 displays correctly, and on button1 i get the error:  A DataSource Instance has not been supplied for DataSource "DataSet2_DataSet2".

    I have the DataSources.Clear() method called, but what else do i need to do to get this to work?

    Thanks 

    Tuesday, November 20, 2007 7:03 PM

Answers

  • User-1846718393 posted

     Thank you for the reply.  In the link you sent me, there is a sample code that performs similar things.  In that code there is a line:

    ReportViewer1.Reset()

    I simply added this to my button_click events before manipulating the ReportViewer and assigning new DataSources, and it solved all the problems.  In my example, not only am I changing/adding new datasource, I am changing the ReportPath "RDLC" file it uses.  This is where my problems were coming in.  I was able to change the datasource with no problem as long as the ReportPath pointed to the same RDLC.  If I changed that, it errored out.  Adding the .Reset() method before all of this was the fix

    Thanks again

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2007 9:27 AM

All replies

  • User-1136466523 posted

    Hi,

    From your description, it seems that you want to change the report at runtime, right?

    I think you can try the following code which works on our machine.

    ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
    ReportViewer1.LocalReport.ReportPath = "C:\\rptPatientList.rdlc";
    ReportViewer1.LocalReport.DataSources.Clear();
    ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportDataSet_PatientLlist", dsReport.Tables[0]));
    ReportViewer1.DocumentMapCollapsed = true;
    ReportViewer1.RefreshReport();

    For more information, you can refer the following threads:
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2421083&SiteID=1

    Thanks.

    Thursday, November 22, 2007 12:19 AM
  • User-1846718393 posted

     Thank you for the reply.  In the link you sent me, there is a sample code that performs similar things.  In that code there is a line:

    ReportViewer1.Reset()

    I simply added this to my button_click events before manipulating the ReportViewer and assigning new DataSources, and it solved all the problems.  In my example, not only am I changing/adding new datasource, I am changing the ReportPath "RDLC" file it uses.  This is where my problems were coming in.  I was able to change the datasource with no problem as long as the ReportPath pointed to the same RDLC.  If I changed that, it errored out.  Adding the .Reset() method before all of this was the fix

    Thanks again

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2007 9:27 AM
  • User1735352362 posted

    in my program i used like this

    Dim p As New System.Collections.Generic.List(Of Microsoft.Reporting.WebForms.ReportParameter)
            Dim RDS As New Microsoft.Reporting.WebForms.ReportDataSource
            Dim RDS1 As New Microsoft.Reporting.WebForms.ReportDataSource

     If Request("Form") = "Report1" Then
                RDS.Name = "reportdatasource"
                RDS.DataSourceId = "DataSource1"
                ReportViewer1.LocalReport.DataSources.Add(RDS)
                ReportViewer1.LocalReport.ReportEmbeddedResource = "Reports\Report1.rdlc"
                ReportViewer1.LocalReport.ReportPath = "Reports\Report1.rdlc"
                p.Add(New Microsoft.Reporting.WebForms.ReportParameter("Division", Request("Param1")))
            End If
            If Request("Form") = "Report2" Then
                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.Refresh()
                RDS1.Name = "reportdatasource"
                RDS1.DataSourceId = "DataSource2"
                ReportViewer1.LocalReport.DataSources.Add(RDS1)
                ReportViewer1.LocalReport.ReportEmbeddedResource = "Reports\Report2.rdlc"
                ReportViewer1.LocalReport.ReportPath = "Reports\Report2.rdlc"
                p.Add(New Microsoft.Reporting.WebForms.ReportParameter("Year", Request("Param1")))
            End If
            ReportViewer1.LocalReport.SetParameters(p)
            ReportViewer1.LocalReport.Refresh()

    i tried this in my program....its working yar..

    hope this will  helps somebody

    thanks.....all d best

    Wednesday, July 13, 2011 7:17 AM
  • User1240199689 posted

    HI . I wrote a code in which I try to change dynamically some report . But it doesn't work and there is a message that there is no data souurce instance for dataset . By the way, I have an objectdatasource called ObjectDataSource1 which gets data from DataSet created while creating a report called Report1

    Here is a code

    Hi, at your post http://forums.asp.net/t/1184635.aspx you wrote a part of some source code , and there was dsReport object . what kind of object is it ? and here is mine code, in which i try to change a report by a button click. So , I have an ObjectDataSource that is connected with DataSet and I created a report called "Report2" . When I start debugging, there is a message "DataSet1 has no data source instances" . What is wrong here ?  

    ReportDataSource rds =new ReportDataSource();
    rds.Name = "rds";
    rds.DataSourceId = "ReportDataSource1";
    rds.Value = ObjectDataSource1;
    Viewer1.LocalReport.DataSources.Clear();
    Viewer1.LocalReport.Refresh();
    Viewer1.LocalReport.DataSources.Add(rds);
    //Viewer1.DocumentMapCollapsed = true;
    // Viewer1.ProcessingMode = ProcessingMode.Local;
    Viewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";
    Viewer1.LocalReport.ReportPath = "Report1.rdlc";

     

    Monday, August 1, 2011 2:00 AM
  • User802050332 posted

    Thanks a lot dear!!

    Your post was useful for me .

     

    Cheers

    Senthil

    Wednesday, October 26, 2011 5:53 AM