fred2028 0 Newbie Poster

Current I am making an application that uses 4 different ReportViewer reports (.rdlc) files, depending on which report the user chooses to view. This choice is from a drop-down box.

Currently, if the user visits the page and then selects the report he wants and clicks View, it works fine. However, when the user changes his selection after seeing a report, and then clicks View, the report area in the page gives an error

A data source instance has not been supplied for the data source 'AutoFinancePage2_3_4_p2'.

where "AutoFinancePage2_3_4p2" is just the data source. Each report option choice is tied to a different RDLC file and a different data source. The code of interest would be:

protected void btnViewReport_Click(object sender, EventArgs e)
    {
        if (ddlPage.SelectedValue == "2")
        {
            ReportParameter[] rg_params = new ReportParameter[2];
            rg_params[0] = new ReportParameter("source_id", ddlSourceId.SelectedValue);
            rg_params[1] = new ReportParameter("host_name", ddlHost.SelectedValue);
            this.ReportViewer1.LocalReport.SetParameters(rg_params);

            // Clears the source data (below) before a new request is sent
            this.ReportViewer1.LocalReport.DataSources.Clear();

            string sql = delinquencyWriteOffs();
            // DB_Connection is stored in connections.config
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            // DataSet1 must match the tables in the RDLC source table
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("AutoFinancePage2_3_4_p2", dt));
        }
        else if (ddlPage.SelectedValue == "3")
        {
            ReportParameter[] rg_params = new ReportParameter[2];
            rg_params[0] = new ReportParameter("source_id", ddlSourceId.SelectedValue);
            rg_params[1] = new ReportParameter("host_name", ddlHost.SelectedValue);
            this.ReportViewer1.LocalReport.SetParameters(rg_params);
            this.ReportViewer1.LocalReport.DataSources.Clear();
            string sql = volumeBonusPaid();
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            // DataSet1 must match the tables in the RDLC source table
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("AutoFinancePage2_3_4_p3", dt));
            /* The following line had to be added since I accidentally used (but then removed) data from p4's table
             * in 3.rdlc */
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("AutoFinancePage2_3_4_p4", dt));
        }
        else if (ddlPage.SelectedValue == "4")
        {
            ReportParameter[] rg_params = new ReportParameter[2];
            rg_params[0] = new ReportParameter("source_id", ddlSourceId.SelectedValue);
            rg_params[1] = new ReportParameter("host_name", ddlHost.SelectedValue);
            this.ReportViewer1.LocalReport.SetParameters(rg_params);

            // Clears the source data (below) before a new request is sent
            this.ReportViewer1.LocalReport.DataSources.Clear();

            string sql = averageTurnaroundTime();
            // DB_Connection is stored in connections.config
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            // DataSet1 must match the tables in the RDLC source table
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("AutoFinancePage2_3_4_p4", dt));
        }
        else // Basically if ddlPage.SelectedValue == "1" or default
        {
            ReportParameter[] rg_params = new ReportParameter[1];
            rg_params[0] = new ReportParameter("source_id", ddlSourceId.SelectedValue);

            this.ReportViewer1.LocalReport.SetParameters(rg_params);
            this.ReportViewer1.LocalReport.DataSources.Clear();

            string sql = CreateSQLcommandForNewUsedBookingsVolume();
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));

            sql = CreateSQLcommandForIndirectRetailVolume();
            dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", dt));

            sql = CreateSQLcommandForCustomerRatio();
            dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet3", dt));

            sql = CreateSQLcommandForBookRatio();
            dt = GenericDataAccess.ExecuteSelectCommand(sql, "DB_Connection");
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet4", dt));
        }

        this.ReportViewer1.ShowReportBody = true;
        this.ReportViewer1.LocalReport.Refresh();

    }

Each case of the IF statement is tested working (which is why when the user chooses the report then clicks View, it works), but for some reason when the user changes his mind, decides to view another report, and clicks the button, the error occurs. What could the reason be?

If the entire C# code is needed, I can sanitize and post it.

Edit: I'm thinking could it be because when the report changes, the data source from the previous report is not removed? I think so because say I choose the report that uses source1 as a data source. Then I select the report that uses source2. When I make the selection, it will give

A data source instance has not been supplied for the data source 'source2'.

as an error, but when I click View, the report seems to parse (takes a few seconds due to SQL query), and then the error becomes:

An error has occurred during report processing.
A data source instance has not been supplied for the data source 'source1'.

Notice how the data source in the error changed?