none
So, how DO you render an rdlc dynamically in a ReportViewer?? RRS feed

  • Question

  • I have read numerous articles and seen many examples of how to dynamically render a report using an rdlc and reportviewer, none of which has resulted in a successful rendering of the report, but just a lot of confusion. I'm hoping someone can clear things up for me and tell me how to do this correctly.

     

    My report (SalesReport.rdlc) has two string parameters, Quarter and AgencyType. Here's the code I'm using:

     

    With ReportViewer1

         .Visible = True

         .Reset()

         .LocalReport.ReportPath = "SalesReport.rdlc"

     

         Dim rdsReport As New ReportDataSource

         With rdsReport

              .Name = "SalesReport"

              .Value = dtReport 'Created earlier in the code

         End With

         .LocalReport.DataSources.Clear()

         .LocalReport.DataSources.Add(rdsReport)

     

         'All RptParam_ variables have been set earlier in the code

         Dim prmQrTitle As New ReportParameter("Quarter", RptParam_Qarter)

         Dim prmAgencyType As New ReportParameter("AgencyType", RptParam_AgencyType)

         Dim parameters() As ReportParameter = {prmQuarter, prmAgencyType}

         .LocalReport.SetParameters(parameters)

         .DataBind()

         .LocalReport.Refresh()

    End With

     

    Here are the problems I am experiencing:

     

    If I use the SmartTag of the ReportViewer to create a dataset, it ends up creating a TableAdapter and an ObjectDataSource. I then assign the resulting DataTable to the DataSetName property of the table in the report. When I run this, it goes through the code without an error, but where the report should be rendered (where the ReportViewer is), I get this error:

     

    A data source instance has not been supplied for the data source 'SalesReport_SalesReportTable'

     

    If I reset (delete the value of) the DataSetName property, I get the same error. If I then open the rdlc in a text error and delete the <DataSets> element and all its contents, I get this error at complie:

     

    Error 1 The table ‘table2’ is in the report body but the report has no data set.  Data regions are not allowed in reports without datasets.

     

    So, I'm very confused. If I am going to assign a DataTable dynamicall (at runtime), do I still need to have the DataSetName property of the report table assigned to a datatable? How can I get away from this situation in which I get errors if I assign a DataTable and I get errors if I don't. How DO I set things up so that I can assign a report to a ReportViewer and a DataTable to the LocalReport in code?

     

    Thank you very much for your help.

    Can someone please tell me how to correctly set things up so that I can dynamically set a report to the reportviewer and dynamically assign a DataTable to the report?

     

    Monday, May 12, 2008 6:15 PM

All replies

  • Hi

    I think that changing

     

    With rdsReport

              .Name = "SalesReport"

              .Value = dtReport

    End With

     

    to

     

    With rdsReport

              .Name = "SalesReport_SalesReportTable"

              .Value = dtReport

    End With

     

    should help and will take little time.

     

    As for explanation...first you need to create dataset in you project (not from SmartTag of the ReportViewer or report designer) - in solution explorer right click on you project and add new item - lets call it 'DataSet1'. Next you need to add table to it. I think the easiest way (of course if have access to database that you will be using in report) is to add tableadapter, then write you query and after all delete tableadapter, this way you will have only datatable (for this example name it 'Table1'). After this in report designer click Report\Data sources and add your newly created data source (its name should be 'DataSet1_Table1'). After placing all needed fields in your report you can dynamically set report data source like you did in your code but ReportDataSource object should have name 'DataSet1_Table1' and proper value. I hope this will help you.

     

    Tuesday, May 13, 2008 8:29 AM