locked
How to dynamically assign ReportViewer to .rdlc RRS feed

  • Question

  • The steps of create a local report are follow:

    1 Inserted a DataSet into my WebApplication and used it as the DataSource of a Report file (Report.rdlc).

    2. Add a new web page, and put a ReportViewer on it.

    3. Set the Report.rdlc to the ReportPath of the ReportViewr by click the smart tag of the ReportViewer, and it will automatically add a OjbetDataSource into the WebPage.

    But I wnat that the ReportViewer does not need bunddle to a special .rdlc file in design time.  How can I accomplish the Step3  with codes?


    Tuesday, November 29, 2005 12:14 PM

Answers

  • I just got this working myself by doing the following:

    1. Set up a report normally and attach it to the report viewer by using the associated tasks menu.
    2. Set up another report.
    3. Go into the designer code (i.e. MainForm.Designer.cs) and delete the following code to unassign the current report (it would be nice to have an option to dissassociate a report like the Crystal  Reports associated task menu has, but this works):

    //reportDataSource1.Name = "Reports_GetAttendanceTotalsReport";
    //reportDataSource1.Value = this.GetAttendanceTotalsReportBindingSource;
    //this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
    //this.reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.AttendanceTotals.rdlc";

    4. In the regular code (i.e. MainForm.cs), add the following to your private members:

    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new         Microsoft.Reporting.WinForms.ReportDataSource();

    5. Then add something similar to this to swap out the reports:

            //display the proper report when the user selects it from the report dropdown menu
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {     
                reportViewer1.Reset();
               
                switch(comboBox1.SelectedItem.ToString()) {
                    case "Attendance":
                        reportDataSource.Name = "Reports_GetAttendanceTotalsReport";
                        reportDataSource.Value = this.GetAttendanceTotalsReportBindingSource;
                        reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.AttendanceTotals.rdlc";
                        reportViewer1.RefreshReport();
                        break;
                    case "Seniority":
                        reportDataSource.Name = "Reports_GetSeniorityReport";
                        reportDataSource.Value = this.GetSeniorityReportBindingSource;
                        reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.Seniority.rdlc";
                        reportViewer1.RefreshReport();
                        break;
                }
            }

    -----------
    Let me know if this doesn't work for you.

    Dave Gruska
    Wednesday, January 4, 2006 3:42 PM

All replies

  • with this code:
     Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "WindowsApplication1.Report1.rdlc"

    modify this code for your project
    hi
    Friday, December 9, 2005 8:29 AM
  • Thanks,
    I am develop Report with WebApplication.

    I modify your code to
    Me.ReportViewer1.LocalReport.ReportEmbeddedResource = MapPath("Report1.rdlc");

     But it result in an error message on run time:

    An error occurred during local report processing.
    The report definition for report 'C:\VS\HealthSolu\Health8\LocalReport\Disease_Class.rdlc' has not been specified 

     How can I do?
    Saturday, December 10, 2005 11:04 PM
  • Hello Bill,

    Here is some code from a bata sample Report Viewer project I am building on top of EntLib 2.0.  It will be a project that can be added to the DataAccessQuickStart sample.  On the form there are 2 buttons and the ReportViewer control.  Below the VB code there is some C# code from another application I am working on.

    You most likly worked this out already, but, If not, send me an email.

    The VB code uses and embeded report and the C# code uses and rdlc file from the file system.

    Public Class ReportForm4

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim rds As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource

    Me.ReportViewer1.Reset() 'Reset control to clear previous report information

    Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "DataAccessQuickStart.rptGetCustomers.rdlc"

    Me.ReportViewer1.LocalReport.DataSources.Clear()
    rds.Name = "dsGetCustomers_GetCustomers"

    rds.Value = SalesData.GetCustomers.Tables[0]
    Me.ReportViewer1.LocalReport.DataSources.Add(rds)

    Me.ReportViewer1.RefreshReport()

    End Sub

    Private Sub ReportForm4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim rds As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource

    Me.ReportViewer1.Reset()

    Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "DataAccessQuickStart.rptGetProductsByCategory.rdlc"

    Me.ReportViewer1.LocalReport.DisplayName = "Get Products By Category"

    Me.ReportViewer1.LocalReport.DataSources.Clear()

    rds.Name = "GetProductsByCategory_GetProductsByCategory"

    rds.Value = SalesData.GetProductsInCategory(2).Tables("table")

    Me.ReportViewer1.LocalReport.DataSources.Add(rds)

    Me.ReportViewer1.RefreshReport()

    End Sub

    End Class

    --
    C#
    --

    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();

    • Proposed as answer by Ignacio Vales Thursday, May 26, 2011 1:36 PM
    Saturday, December 31, 2005 8:32 PM
  • I just got this working myself by doing the following:

    1. Set up a report normally and attach it to the report viewer by using the associated tasks menu.
    2. Set up another report.
    3. Go into the designer code (i.e. MainForm.Designer.cs) and delete the following code to unassign the current report (it would be nice to have an option to dissassociate a report like the Crystal  Reports associated task menu has, but this works):

    //reportDataSource1.Name = "Reports_GetAttendanceTotalsReport";
    //reportDataSource1.Value = this.GetAttendanceTotalsReportBindingSource;
    //this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
    //this.reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.AttendanceTotals.rdlc";

    4. In the regular code (i.e. MainForm.cs), add the following to your private members:

    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new         Microsoft.Reporting.WinForms.ReportDataSource();

    5. Then add something similar to this to swap out the reports:

            //display the proper report when the user selects it from the report dropdown menu
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {     
                reportViewer1.Reset();
               
                switch(comboBox1.SelectedItem.ToString()) {
                    case "Attendance":
                        reportDataSource.Name = "Reports_GetAttendanceTotalsReport";
                        reportDataSource.Value = this.GetAttendanceTotalsReportBindingSource;
                        reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.AttendanceTotals.rdlc";
                        reportViewer1.RefreshReport();
                        break;
                    case "Seniority":
                        reportDataSource.Name = "Reports_GetSeniorityReport";
                        reportDataSource.Value = this.GetSeniorityReportBindingSource;
                        reportViewer1.LocalReport.DataSources.Add(reportDataSource);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "AttendanceTracking.Seniority.rdlc";
                        reportViewer1.RefreshReport();
                        break;
                }
            }

    -----------
    Let me know if this doesn't work for you.

    Dave Gruska
    Wednesday, January 4, 2006 3:42 PM
  • Thank,

    But I do with Web Applicaiton.

    Can it work with Web application.

    I can find any file like MainForm.Designer.cs in my Web application

    Wednesday, January 4, 2006 10:12 PM
  • Ryan,

    This is how is associate a report to a report viewer control and bind it to an instantiated object datasource (i.e. m_TaskTrackerRpt):

    If Not Page.IsPostBack Then
      
    Me.ReportViewer1.LocalReport.ReportPath = "RptTaskTracker.rdlc"
      
    Me.ReportViewer1.LocalReport.DataSources.Clear()
      
    Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("TaskTrackerData", m_TaskTrackerRpt.GetTaskTrackerData()))
      
    Me.ReportViewer1.DataBind()
    End If

    Hope this helps.

    Friday, January 20, 2006 4:07 PM
  • One way you can change reports at runtime is using the reset function and ReportPath property together...

     

     

    Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim dtTable As Data.DataTable

    Dim rds As Microsoft.Reporting.WebForms.ReportDataSource

    'Important: Clear the OLD report

    ReportViewer1.Reset()

    ReportViewer1.LocalReport.Dispose()

    'NOTE:  take values from a configuration file

    Select Case LstReportNames.Selectedvalue

    Case "Cars"

    'GET dtTable here

    rds = New Microsoft.Reporting.WebForms.ReportDataSource("Table1", dtTable)

    ReportViewer1.LocalReport.DataSources.Clear()

    ReportViewer1.LocalReport.DataSources.Add(rds)

    ReportViewer1.LocalReport.ReportPath = "Reports\cars.rdlc"

    Dim rptParam As New Microsoft.Reporting.WebForms.ReportParameter("Report_Parameter_Name", ReportNames1.SelectedReport)

    ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rptParam})

    ReportViewer1.DataBind()

    Exit Select

    Case "Rocket"

    'GET dtTable here

    rds = New Microsoft.Reporting.WebForms.ReportDataSource("ByReport_TableByReport", dtTable)

    ReportViewer1.LocalReport.DataSources.Clear()

    ReportViewer1.LocalReport.DataSources.Add(rds)

    ReportViewer1.LocalReport.ReportPath = "Reports\Rocket.rdlc"

    Dim rptParam As New Microsoft.Reporting.WebForms.ReportParameter("Report_Parameter_Name", ReportNames1.SelectedReport)

    Dim rptParamGroupBy As New Microsoft.Reporting.WebForms.ReportParameter("paramGroupBy", "GroupColumn1")

    ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rptParam, rptParamGroupBy})

    ReportViewer1.DataBind()

    Exit Select

    End Select

    ReportViewer1.LocalReport.Refresh()

     

    ' rds.

     

    ReportViewer1.Visible = True

    ReportViewer1.ShowReportBody = True

    End Sub

    Friday, November 16, 2007 9:42 PM
  • Your code above is simple, but I am trying to drill through to the same report and query historical information on the drill through.  Thoughts. 

     

    Right now, on the drill through event, I have: 

      Private Sub ReportViewer1_Drillthrough(ByVal sender As Object, ByVal e As Microsoft.Reporting.WinForms.DrillthroughEventArgs) Handles ReportViewer1.Drillthrough
        Dim Filter As String = "", dt As New DataTable
        'Me.ReportViewer1.Reset()
        Me.ReportViewer1.ProcessingMode = ProcessingMode.Local
    
        Me.HousesTableAdapter.Fill(Me.Database1DataSet.Houses)
        Me.Database1DataSet.Houses.DefaultView.RowFilter = ""
    
        dt = Me.Database1DataSet.Houses.DefaultView.ToTable
        Me.HousesBindingSource.DataSource = dt
    
        Dim x As New ReportDataSource
        x.Name = "DataSet1"
        x.Value = Me.HousesBindingSource
    
        Me.ReportViewer1.LocalReport.DataSources.Add(x)
        Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "SoldHouses.rdlc"
        Me.ReportViewer1.Refresh()
      End Sub
    

    I am launching the report viewer with this code:

        RU.HousesTableAdapter.Fill(RU.Database1DataSet.Houses)
        RU.Database1DataSet.Houses.DefaultView.RowFilter = ""
        RU.Database1DataSet.Houses.DefaultView.RowFilter = Filter
    
        dt = RU.Database1DataSet.Houses.DefaultView.ToTable
    
        RU.ReportViewer1.LocalReport.DataSources.Clear()
        RU.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", dt))
        RU.Show()
    
    It works correctly.  This has been very frustrating. 
    Thursday, August 12, 2010 3:29 AM
  •  

     

    I'm trying to dynamically add all datasources I find in an rdl as ReportDataSources but I'm getting an error on the last one:

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

     

    even though upon debugging the report I can see that the datasource name has been found and added:

     

                 foreach (XmlNode node2 in nodes2)
                  {
                      XmlNodeList multinode = xmldoc.GetElementsByTagName("DataSet");
                      foreach (XmlNode node3 in multinode)
                      {
                          mdsName = node3.Attributes["Name"].InnerText;
                          Microsoft.Reporting.WebForms.ReportDataSource mrds = new Microsoft.Reporting.WebForms.ReportDataSource(TableOrViewName, dt);
                          mrds.Name = mdsName;
                          ReportViewer1.LocalReport.DataSources.Add(mrds);
                          if (mdsName == "sql_cluster")
                          {
                              testparam = "2010-06-30";
                          }

                      }
                  }
              }
              else
              {
                  XmlNodeList nodes3 = xmldoc.GetElementsByTagName("DataSource");
                  //Now I need to get the xml string into a sql statement
                  foreach (XmlNode node3 in nodes2)
                  {
                      dsName = node3.Attributes["Name"].InnerText;
                  }
              }
              if (InSqlStmt == "")
              {
                  cmd = new SqlCommand(sqlStmt, con);
                  if (testparam != "")
                  {
                      SqlParameter param = new SqlParameter();
                      param.ParameterName = "@Period";
                      param.Value = testparam;
                      cmd.Parameters.Add(param);
                      dsName = "sql_cluster";
                  }

              }
              else
              {
                  cmd = new SqlCommand(InSqlStmt, con);
              }
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);
              ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
              ReportViewer1.LocalReport.ReportPath = ReportFile;
              ReportViewer1.LocalReport.DataSources.Clear();
              Microsoft.Reporting.WebForms.ReportDataSource rds = new Microsoft.Reporting.WebForms.ReportDataSource(TableOrViewName, dt);
              rds.Name = dsName;
              ReportViewer1.LocalReport.DataSources.Add(rds);
              ReportViewer1.DocumentMapCollapsed = true;
              ReportViewer1.LocalReport.Refresh();
          }
    I agree with RoadRunnerSam this is very frustrating... Does anybody know what I'm doing wrong to get this error??

    Thanks

    Dean

    Wednesday, November 3, 2010 10:11 PM
  • I am working on reports and i have to print each person data on a separate page I have writen a code in vb.net(WebApp) that gets each person data from database and sets DataSet but when i set ReportDataSource with that DataSet it shows only last record replacing all the previous rather than adding new page to show 2nd record and so on.. Can anyone please guide me to have each record on separate page in report.
     Public Sub CustomReportShow(ByVal dset As DataSet)
          
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", dset.Tables(0)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet2", dset.Tables(1)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet3", dset.Tables(2)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet4", dset.Tables(3)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet5", dset.Tables(4)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet6", dset.Tables(5)))
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet7", dset.Tables(6)))
            ReportViewer1.LocalReport.ReportPath = "Report.rdlc"
            ReportViewer1.LocalReport.Refresh()
        End Sub

    Thursday, May 22, 2014 4:51 AM