Link to home
Start Free TrialLog in
Avatar of Kevin Kilgore
Kevin Kilgore

asked on

visual basic 2010 ReportViewer question

I've searched the web but can't find anything that works yet for this question:

I'm creating a report window in my vb 2010 program and give users options to select 2 different reports to run.  The ReportViewer Control only let's you run one report at a time.  How would i code this so that depending on which report the user selects, the single report viewer control will display the desired report based on user selection?

thanks!
Avatar of puffdaddy411
puffdaddy411

I use a combo box which displays the names of the available reports.  Depending upon the name selected, I load a different report.  You can use anything and simply add code to the changed event.

Something like this....

MainReportViewer.LocalReport.DataSources.Add(YourDataSourceInfo)
MainReportViewer.LocalReport.ReportPath = YourReportPath
'Add any parameters you have
MainReportViewer.RefreshReport
Avatar of Kevin Kilgore

ASKER

Gotcha...sorry, i'm new to this....on the part where you want me to put in "YourDataSourceInfo", what exactly needs to go there?  something like "me.MyDataSet" or "me.MyDataSet.MyBindingSource" ?  Also, should the "YourReportPath" be in quotations in the code?

thanks!
This all depends on how you set up your reportviewer object.  Are you dynamically loading your current report by setting the report path in code or did you select the report from the reportviewer control on your form?

Are your reports embedded in your project or are they stored in an external file location (C:|Reports or something)?

How is your current datasource set up?
At first i selected the report from the reportviewer control on my form but that did me no good since i need to display two reports at will instead of only one.  So i took that off...reports are embedded in the project.... on the report window form, i have 1 dataset, 1 table adapter, and 1 binding source....that's all so far....
Ok.  You may not need to do anything with the datasource in code since you have it set on the form.  Just use this to load different reports for embedded report.rdlc files.


MainReportViewer.LocalReport.ReportEmbeddedResource = "YourProjectName.ReportName.rdlc"
MainReportViewer.RefreshReport


If your project is named "Project1" and your report is named "RPT1.rdlc" then it's as simple as that.  

MainReportViewer.LocalReport.ReportEmbeddedResource = "Project1.RPT1.rdlc"
Ok i think we are getting somewhere, i tried the suggestion above and it refreshed the report or at least it changed it to the one i wanted but it didn't generate the report because it had this error message:

"A Data Source Instance has not been supplied for the Data Source "MyReport.rdlc"
....meant to say the reportviewer came up with that error message ....
Ok.  You just need to set your datasource/bindingsource each time you refresh the control.  Whatever you are using that is on your form, we are going to set that in code.

MainReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSourceName", myBindingSource)
MainReportViewer.LocalReport.ReportEmbeddedResource = "YourProjectName.ReportName.rdlc"
MainReportViewer.RefreshReport

Ok, so I put the code in but it is underlining the "ReportDataSource" part and saying "Report Data Source is not definied"....Am I doing something wrong?  Here is my code:

 ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("area51DataSet", InventoryBindingSource)ReportViewer1.LocalReport.ReportEmbeddedResource = "Area_51.r_SerialID.rdlc"
 ReportViewer1.RefreshReport()

I have attached the actual code so you can see....
error-code.JPG
Add this to the top of your code page.

Imports Microsoft.Reporting.Winforms

OR

Add Microsoft.Reporting.Winforms. before Reportdatasource.
Tried both, still getting same error...i've attached what my form looks like and the code behind it...maybe that will help a little...thanks!
Public Class w_ReportCenter





    Private Sub w_ReportCenter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'area51DataSet.Inventory' table. You can move, or remove it, as needed.
        Me.InventoryTableAdapter.Fill(Me.area51DataSet.Inventory)
        Me.ReportViewer1.RefreshReport()

    End Sub

    
 

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        Me.Close()

    End Sub

    Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click

        If cboArea.Text = "Inventory" And cboReport.Text = "Master Report" Then




            ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("area51DataSet", InventoryBindingSource))
            ReportViewer1.LocalReport.ReportEmbeddedResource = "Area_51.r_SerialID.rdlc"
            ReportViewer1.RefreshReport()



        End If

    End Sub
End Class

Open in new window

form.JPG
I also left the coding out of the combo boxes so it wouldn't crowd the code i just gave you since it was a larger portion of code...just loading lists in the combo boxes...
Sorry for the delay.  Busy few days.

Can you take a screenshot of the data schema that is shown when you are viewing the rdlc designer?  We may have to recreate your reportviewer object.  We are trying to dynamically assign items but the way it is set up there on your form is a static method.  I would just like to see what is available to the report in the report designer.
Is this the right screen you are referring to?
project.PNG
ASKER CERTIFIED SOLUTION
Avatar of puffdaddy411
puffdaddy411

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the info, I'll try this as soon as I get back to town today
Great! What was your final code?