Report viewer is a tool that you can use to generate a report on a ASP.NET page. This post will discuss how it can be used.
To start, we will drag and drop the Report Viewer from the toolbox to the ASPX page. In order for the report viewer to work, we also need to drag and drop a Script Manager, since the Report Viewer utilize Ajax and the Script Manager manages the asynchronous communication between the client (report viewer) and the server.
Now we can design our report by click on the right arrow on the top right of the report viewer component. Then, select “Design a new report” option.
During the report design process, you will be prompted to select a dataset/data soruce. It must be created prior to the report creation. The dataset/data source will be used for the reporting data source.
In the actual page design, you can drag and drop data fields from the data source on to the report page. Once you have completed your design, you can save it and be sure that the report path property has set.
Remember that all the report generated are local, unless you gathering data from a remote server source. The discussion about this type of report is beyond the scope.
If for some reasons that your report is not appearing, or the report viewer keeps on refreshing itself during the runtime, you might want to switch off the AsyncRedering property of the report.
Now lets discussion, how can we assigned the the data source dynamically during the run time. This is useful when you want to generate a report with a query that retrieve new data from the database.
D BConnectionManager d bm = new D BConnectionManager();\n OleD bCommand cmd = d bm.getCommand();\n cmd.CommandText = @"SELECT * FROM MYTABLE WHERE ID=" + this.txtID.Text;\n OleD bDataAdapter da = new OleD bDataAdapter(cmd);\n DataTable dt = new DataTable();\n da.Fill(dt);\n int i = dt.Rows.Count;\n this.rptViewer.LocalReport.ReportPath = "Report1.rdlc";\n this.rptViewer.LocalReport.DataSources.Clear();\n this.rptViewer.LocalReport.DataSources.Add(new ReportDataSource("DSRequestStatus", dt));\n this.rptViewer.LocalReport.Refresh();
Line 1 to 6 is just a normal data retrieval sequence for filling a dataset from an data adapter.
Line 8 define the report path. We can use it to dynamically assign different type of report to a single report viewer during the run time.
Line 10 add a data source to the report viewer. The ReportDatasource object has 2 parameters, the first one is the name of the dataset data source you given previously during the report setup. See the screen shot below. “MyDataSet” is the name of the data source. So if we create a new report data source, we can use the following statement.
new ReportDataSource("MyDataSet", dt) //fill my dataset with a data table.