I have read every single SO question and online article regarding this, and I get confused in a couple different instances.
As my project sits, I have tried to create a Report (Report2.rdlc
) manually and drag different fields from DataSources
onto the report, and use that report as the data source for the ReportViewer
. This did not work. I need to use the DataTable
I created from a SqlDataAdapter
because it decrypts specific rows. I tried creating a DataSet
and filling it with the DataTable
, however I was unable to perform that, also.
- I don't understand: if I have a
ReportViewer
control on a
WinForm
, what all I need in my code to bind a data source to it.
- Is the code even creating a report and utilizing the report for
the
ReportViewer
?
The DataTable
name is dt
and the ReportViewer
control is rv1
.
Here's the code I have been toying around with, however I am not sure what to put as the Report Path
Dim RDS1 As ReportDataSource = New ReportDataSource("Test Report", dt)
rv1.LocalReport.DataSources.Clear()
rv1.ProcessingMode = ProcessingMode.Local
rv1.LocalReport.EnableExternalImages = True
rv1.LocalReport.ReportEmbeddedResource = "Your Report Path"
rv1.LocalReport.DataSources.Add(RDS1)`.
The worst part is, the ReportViewer
just shows up blank. There are no errors or any indicators as to what could be going wrong.
The information within the DataTable
dt
is all correct (verified by viewing it in a DGV
). I am just trying to use that data in a Report
/ ReportViewer
.
Does anyone have any advice? I cannot seem to catch a break on this issue.
Note: Exporting to Excel is not an option. There are encrypted values that require decryption. The report needs to be printable.
Edit: Here is how I populate the DataTable:
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = @searchFirst " & _
"OR LAST_NM_TXT = @searchLast"
cmd.Parameters.AddWithValue("@searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("@searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
So, here we have the DataTable with the correct data in it. I have then gone to the Project Name, Added a new Report (Report1.rdlc), and from here I am unsure of the next steps. If I create a DataSet dynamically, should I see it in this window?
Since this seems to be a popular thread, I'll explain how I was able to do this in quick/easy steps.
- Right click on your porject -> Add New Item -> Selection Report.rdlc
- Top left (Data Source) -> New -> Dataset (select Database [Next ->] Dataset [Next ->] Your DB connection.
- "Choose your database objects" screen -> Select Tables
- "Choose the dataset" screen -> This will be reset at run time. Make sure you remember the name of this Dataset, because it will be used in the code.
- Add ReportViewer control (under Reporting) to the form. Select reportviewer control and go to properties (bottom right pane in VS). Select Local Report's property and set ReportEmbeddedResource to point to the report path we just created. ** ProjectName.ReportName.rdlc**
Peform the usual:
Public DataSet FillDS()
//Try Catch block & other code omitted
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = @searchFirst " & _
"OR LAST_NM_TXT = @searchLast"
cmd.Parameters.AddWithValue("@searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("@searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
Then, we bind the datasource at run time.
DataSet yourDS = FillDS();
ReportDataSource rds = New ReportDataSource("YourDataSetNameFromStep4", yourDS.Tables[0]);
yourReportViewerName.localreport.datasources.clear();
yourReportViewerName.localreport.datasources.add(rds);
yourReportViewerName.refreshreport();
If there is something I could post in here to help anyone else, let me know.
See Question&Answers more detail:
os