none
Loading 2 different reports in a report viewer RRS feed

  • Question

  • I have been playing with this for a while now and I havent fiqured out how to reattach the datasource after one report is removed and another is loaded

     

    Code Snippet

    If whichReport = "Contact Information" Then

    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 = "EmployeeBindingSource"

    rds.Value = EmployeeBindingSource

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

    ReportViewer1.LocalReport.ReportEmbeddedResource = "EMP.EmployeeContact.rdlc"

    Me.ReportViewer1.RefreshReport()

    ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)

    ReportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth

    End If

    If whichReport = "Information Report" Then

    ReportViewer1.LocalReport.DataSources.Clear()

    ReportViewer1.LocalReport.ReportEmbeddedResource = "EMP.EmployeeInfo.rdlc"

    Dim custDV As DataView = New DataView(Me.EMPDataSet.Employee, "RecordDeleted='No'", "EName", DataViewRowState.CurrentRows)

    EmployeeBindingSource.DataMember = ""

    EmployeeBindingSource.DataSource = custDV

    ReportViewer1.RefreshReport()

    ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)

    ReportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth

    End If

     

     

    What am I not doing right?

     

    Davids Learning

    Friday, April 25, 2008 5:45 AM

Answers

  • This is what I did for the final project.

     

    I created a table and stored everthing I needed for my reports and shortened my code to accomodate. Its basically a form with a reportviewer (no report defined in the viewer), 2 textboxes for search date and one text box for a search on various fields.

     

     

    Why would I need to call "Reset" if I am creating a new instance everytime?

    This works fine everytime

     

    Public Class EMP_Reports

    Public whichReport As String

    Dim whatTable As String

    Public currentFilter As String

    Dim datesApplied As String = "No"

    Dim searchApplied As String = "No"

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

    Dim ReportDataSourceX = New Microsoft.Reporting.WinForms.ReportDataSource()

    Try

    Me.MyReportsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.MyReportsTableAdapter.Fill(Me.EMPData.MyReports)

    Dim loadReport As EMPData.MyReportsRow

    loadReport = Me.EMPData.MyReports.FindByMyReportName(whichReport)

    whatTable = loadReport.DataTable

    Label3.Text = loadReport.Label3Text

    Label3.Refresh()

    If loadReport.HideTableLayoutPanel = "Yes" Then

    Me.Controls.RemoveByKey("TableLayoutPanel2")

    End If

    If loadReport.ShowDateSelection = "No" Then

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox2")

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox3")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Button1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label2")

    End If

    If loadReport.ShowSearchBox = "No" Then

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label3")

    End If

    Me.Refresh()

    If loadReport.DataTable = "TimeSheets" Then

    Me.TimeSheetsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.TimeSheetsTableAdapter.Fill(Me.EMPData.TimeSheets)

    BindingSource1.DataMember = "TimeSheets"

    End If

    If loadReport.DataTable = "Employee" Then

    Me.EmployeeTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeTableAdapter.Fill(Me.EMPData.Employee)

    End If

    If loadReport.DataTable = "EmployeeEvents" Then

    Me.EmployeeEventsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeEventsTableAdapter.Fill(Me.EMPData.EmployeeEvents)

    currentFilter = "CreatedBy ='" & My.Settings.unam & "'"

    End If

    ReportDataSourceX.Name = loadReport.ReportDataSource

    ReportDataSourceX.Value = BindingSource1

    ReportViewer1.LocalReport.ReportEmbeddedResource = loadReport.ReportName

    Dim custDV As DataView = New DataView(Me.EMPData.Tables(loadReport.DataTable), currentFilter, "", DataViewRowState.CurrentRows)

    BindingSource1.DataMember = ""

    BindingSource1.DataSource = custDV

    ReportViewer1.LocalReport.DataSources.Add(ReportDataSourceX)

    ReportViewer1.RefreshReport()

    ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)

    ReportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth

    Me.Cursor = Cursors.Arrow

    Catch ex As Exception

    Dim x As New LogError

    x.RichTextBox2.Text = "An error has occured & vbCrLf & ex.Message.ToString

    x.Show()

    Log(ex.Message & vbCrLf & Me.Name & vbCrLf & sender.ToString)

    Me.Cursor = Cursors.Arrow

    Me.Close()

    Exit Sub

    End Try

    End Sub

    Saturday, May 3, 2008 1:36 AM
  • Hey, I'm in the same boat with you, just a different part of the river heh.

     

    Report Parameters can be handy for a lot of things,  in my reports I use them for things like toggling visiblity, displaying dynamic group labels and a few other things... here is the basic way to use them.

     

    First you want to create the parameter in your .RDLC file.  To do this, open the report and in the blank area, right click and choose "Report Parameters"

     

    the page is pretty intuitive, but things of note...Prompt is only used for .RDL reports, not .RDLC  and the name you give the parameter is naturally important since that will be the name you  reference in your code, and in the report expressions.

     

    So lets say you create a string parameter named  ReportDescription that you intend on using in the header to dynamically display a title for the report the the user possibly put in a text box or something.

     

    In the repot...to use this parameter once you created it, you simply use the expression

     

    =Parameters!ReportDescription.Value

     

     

    that's all it takes for creating and using the parameter in the report...here is the code for passing the parameter to the report

     

    VB.NET

     

    Code Snippet

    Dim reportDesc As New ReportParameter("ReportDescription", txtReportTitle.Text)

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

     

     

    C#

    Code Snippet

    ReportParameter reportDesc = new ReportParameter("ReportDescription", txtReportTitle.Text);

     

    ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {reportDesc});

     

     

     

     

    Just keep in mind, if you create a parameter in your report, then the report won't load if you don't provide the parameter.  So in the case of my .RDLC i generally use the same parameters in all reports, that way I don't have to dig through the .RDLC and dynamically handle its parameters before loading it,  call me lazy hehe.

     

    Wednesday, May 7, 2008 5:32 PM

All replies

  • After many hours of trying to figure out where I was going wrong and reading several hundred of threads about this, I finally thought of something that might work.

     

    GET RIDE OF THE REPORTVIEWER THAT WAS CREATED ON THE FORM

     

    And

    Create a new instance of the reportviewer each time I wanted a different report. Been playing with it for a while and shes doing fine.

     

    For all of you who are new to the reporting tools, hope someone comes accross this thread, hopefully this will save you alot of time. It seems like everyone in general has the same issues with this tool.

     

    Heres what I done.

    I created a way to convey which report I wanted to view to a blank form, basically another control with a treeview that has a database with a list of all of the reports names(later on after I fiqure parameters out, I will store them there as well).

    Then on the form where I want the report to show up on I am doing this.

     

    Code Snippet

    Public Class EMP_Reports

    Public whichReport As String     'this is set from the form with the treeview on it

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

    Try

    Me.EmployeeTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeTableAdapter.Fill(Me.EMPDataSet.Employee)

    Me.EmployeeEventsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeEventsTableAdapter.Fill(Me.EMPDataSet.EmployeeEvents)

    Me.Controls.RemoveByKey("ReportViewer")

    Me.Cursor = Cursors.Arrow

    Dim ReportViewer As New Microsoft.Reporting.WinForms.ReportViewer

    Dim ReportDataSourceX = New Microsoft.Reporting.WinForms.ReportDataSource()

    If whichReport = "Contact Information" Then

    ReportDataSourceX.Name = "EMPDataSet_Employee"

    ReportDataSourceX.Value = Me.EMPDataSet.Employee

    ReportViewer.LocalReport.ReportEmbeddedResource = "EMP.EmployeeContact.rdlc"

    GoTo line1

    End If

    If whichReport = "Information Report" Then

    ReportDataSourceX.Name = "EMPDataSet_Employee"

    ReportDataSourceX.Value = Me.EMPDataSet.Employee

    ReportViewer.LocalReport.ReportEmbeddedResource = "EMP.EmployeeInfo.rdlc"

    GoTo line1

    End If

    If whichReport = "Tardiness Report" Then

    ReportDataSourceX.Name = "EMPDataSet_EmployeeEvents"

    ReportDataSourceX.Value = Me.EMPDataSet.EmployeeEvents

    ReportViewer.LocalReport.ReportEmbeddedResource = "EMP.EmployeeTardy.rdlc"

    GoTo line1

    End If

    line1:

    ReportViewer.Name = "ReportViewer"

    ReportViewer.Dock = DockStyle.Fill

    Me.Controls.Add(ReportViewer)

    ReportViewer.LocalReport.DataSources.Add(ReportDataSourceX)

    ReportViewer.RefreshReport()

    ReportViewer.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)

    ReportViewer.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth

    Catch ex As Exception

    Dim x As New LogError

    x.RichTextBox2.Text = "An error has occured and a log is being created. Below is a description of the event that occured in the form named " & Me.Name

    x.RichTextBox1.Text = "Exception: " & vbCrLf & ex.Message.ToString

    x.Show()

    Log(ex.Message & vbCrLf & Me.Name & vbCrLf & sender.ToString)

    Me.Cursor = Cursors.Arrow

    Exit Sub

    End Try

    End Sub

    End Class

     

     

    I hope someone who has the same problems as I did runs accross this thread,it might save you several hours of researching what should be something simple to resolve.

     

    Thanks

    Davids Learning

    • Proposed as answer by bsopchak Friday, January 7, 2011 6:06 PM
    Friday, April 25, 2008 5:54 PM
  • I often have similar problem Sad

     

    I use a different solution: all reports in the same form uses the same datasource.

     

    Datasource contains all fields needed for each report, using this datasource I can build different reports and change out of all using only: ReportViewer.LocalReport.ReportEmbeddedResource = "report to be shown.rdlc"

     

    I also have to load in datasource the correct data, but this is fast way to switch report shown.

     

    C.
    Monday, April 28, 2008 8:25 AM
  • You need to call ReportViewer.Reset() before setting the new report definition.

    • Proposed as answer by Kuti Monday, August 11, 2008 5:35 PM
    Thursday, May 1, 2008 6:28 AM
    Moderator
  • This is what I did for the final project.

     

    I created a table and stored everthing I needed for my reports and shortened my code to accomodate. Its basically a form with a reportviewer (no report defined in the viewer), 2 textboxes for search date and one text box for a search on various fields.

     

     

    Why would I need to call "Reset" if I am creating a new instance everytime?

    This works fine everytime

     

    Public Class EMP_Reports

    Public whichReport As String

    Dim whatTable As String

    Public currentFilter As String

    Dim datesApplied As String = "No"

    Dim searchApplied As String = "No"

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

    Dim ReportDataSourceX = New Microsoft.Reporting.WinForms.ReportDataSource()

    Try

    Me.MyReportsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.MyReportsTableAdapter.Fill(Me.EMPData.MyReports)

    Dim loadReport As EMPData.MyReportsRow

    loadReport = Me.EMPData.MyReports.FindByMyReportName(whichReport)

    whatTable = loadReport.DataTable

    Label3.Text = loadReport.Label3Text

    Label3.Refresh()

    If loadReport.HideTableLayoutPanel = "Yes" Then

    Me.Controls.RemoveByKey("TableLayoutPanel2")

    End If

    If loadReport.ShowDateSelection = "No" Then

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox2")

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox3")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Button1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label2")

    End If

    If loadReport.ShowSearchBox = "No" Then

    Me.TableLayoutPanel2.Controls.RemoveByKey("TextBox1")

    Me.TableLayoutPanel2.Controls.RemoveByKey("Label3")

    End If

    Me.Refresh()

    If loadReport.DataTable = "TimeSheets" Then

    Me.TimeSheetsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.TimeSheetsTableAdapter.Fill(Me.EMPData.TimeSheets)

    BindingSource1.DataMember = "TimeSheets"

    End If

    If loadReport.DataTable = "Employee" Then

    Me.EmployeeTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeTableAdapter.Fill(Me.EMPData.Employee)

    End If

    If loadReport.DataTable = "EmployeeEvents" Then

    Me.EmployeeEventsTableAdapter.Connection.ConnectionString = My.Settings.EMPConnection1

    Me.EmployeeEventsTableAdapter.Fill(Me.EMPData.EmployeeEvents)

    currentFilter = "CreatedBy ='" & My.Settings.unam & "'"

    End If

    ReportDataSourceX.Name = loadReport.ReportDataSource

    ReportDataSourceX.Value = BindingSource1

    ReportViewer1.LocalReport.ReportEmbeddedResource = loadReport.ReportName

    Dim custDV As DataView = New DataView(Me.EMPData.Tables(loadReport.DataTable), currentFilter, "", DataViewRowState.CurrentRows)

    BindingSource1.DataMember = ""

    BindingSource1.DataSource = custDV

    ReportViewer1.LocalReport.DataSources.Add(ReportDataSourceX)

    ReportViewer1.RefreshReport()

    ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)

    ReportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth

    Me.Cursor = Cursors.Arrow

    Catch ex As Exception

    Dim x As New LogError

    x.RichTextBox2.Text = "An error has occured & vbCrLf & ex.Message.ToString

    x.Show()

    Log(ex.Message & vbCrLf & Me.Name & vbCrLf & sender.ToString)

    Me.Cursor = Cursors.Arrow

    Me.Close()

    Exit Sub

    End Try

    End Sub

    Saturday, May 3, 2008 1:36 AM
  • wow, that's really a lot more work then you need to do in order for this stuff to work.  Your first code example was a bit more on par, just needed a few changes.   But hey, if all this code works for you, and speed/performance isn't an issue, then go for it.

     

    a couple of recommendations

     

    when creating the .RDLC files, unless there is a reason why you need to have very specific names for the datasources in the .RDLC file, i recommend using a uniform name.  That way you don't have to write code to make sure your getting the name of the datasource correct.  For example, you could name your reportdatasources

     

    ReportData1

    ReportData2

    ReportData3

     

     

    Also, the reports themselves are very efficent at sorting/filtering, consider doing it in the report by passing parameters into your report.

    Monday, May 5, 2008 1:21 PM
  • Hey thanks for your input on this, I am a novice programmer who is self taught, eventually I will know what you know, but until then I have to stick to guns. I learn from people like you, so thanks for sharing this with me. The program I am playing with is like a book, its teaching me want I am wanting to learn before I start my final project.

     

    By the way, I still havent learned the fundamentals of parameters, could you shine a little light on that subject for me?

     

    Say if you had a column in a database(people) that you wanted to return a value(a name), how would you pass the parameter from code to apply that to the exist report named(mypeople).

     

    Thanks

    Davids Learning 

     

    Monday, May 5, 2008 4:09 PM
  • Hey, I'm in the same boat with you, just a different part of the river heh.

     

    Report Parameters can be handy for a lot of things,  in my reports I use them for things like toggling visiblity, displaying dynamic group labels and a few other things... here is the basic way to use them.

     

    First you want to create the parameter in your .RDLC file.  To do this, open the report and in the blank area, right click and choose "Report Parameters"

     

    the page is pretty intuitive, but things of note...Prompt is only used for .RDL reports, not .RDLC  and the name you give the parameter is naturally important since that will be the name you  reference in your code, and in the report expressions.

     

    So lets say you create a string parameter named  ReportDescription that you intend on using in the header to dynamically display a title for the report the the user possibly put in a text box or something.

     

    In the repot...to use this parameter once you created it, you simply use the expression

     

    =Parameters!ReportDescription.Value

     

     

    that's all it takes for creating and using the parameter in the report...here is the code for passing the parameter to the report

     

    VB.NET

     

    Code Snippet

    Dim reportDesc As New ReportParameter("ReportDescription", txtReportTitle.Text)

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

     

     

    C#

    Code Snippet

    ReportParameter reportDesc = new ReportParameter("ReportDescription", txtReportTitle.Text);

     

    ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {reportDesc});

     

     

     

     

    Just keep in mind, if you create a parameter in your report, then the report won't load if you don't provide the parameter.  So in the case of my .RDLC i generally use the same parameters in all reports, that way I don't have to dig through the .RDLC and dynamically handle its parameters before loading it,  call me lazy hehe.

     

    Wednesday, May 7, 2008 5:32 PM
  • "reportViewerXY.Reset();"

    Well, thats the simple and effective answer to the original question... why isn't is marked as such?

    thanks anyway!
    Thursday, May 8, 2008 1:12 PM