RDLC reports cannot have connection string and can't directly connected to the database , instead we use the "Object" datasource to provide data to the reports.
The "Object" datasource provides, at the design time , the report by the structure of the data that will be passed to the report at the runtime
The datasources cannot be added to the MVC project so we need a class library that the datasources will be built upon
Steps
- create a class library project
- add the "ViewModel" class -- that will have the structure of the data
- for example
public class PatientViewModel
{
public int Id{get;set;}
public string Name{get;set;}
}
4. at the solution explorer , select the ViewModelClassLibrary project
5. at the menu bar => Data => select =>Add New DataSource
6. Data Source configuration wizard open => select "Object" Data source =>Next
7. select "PatientViewModel " class => finish
8.create the MVC project
9. add new folder called "forms" at the project main folder
10. add new rdlc report to the folder
11 . open the report => at the view menu => press " report data"
12 .create new datasets => in the list , you will find the created data source at point (7)
and give the dataset a specific name => for example => PatientDataSet
13 . at the controller , create an action for the report
public ActionResult GetReport()
{
LocalReport rep = new LocalReport();
rep.ReportPath = Server.MapPath("~/Forms/MyReport.rdlc");
List<PatientViewModel> patients = GetPatients();
rep.DataSources.Add(new ReportDataSource("PatientDataSet", patients));
//Render the report
_renderedBytes = rep.Render(
_reportType,
_deviceInfo,
out _mimeType,
out _encoding,
out _fileNameExtension,
out _streams,
out _warnings);
return File(_renderedBytes, _mimeType);
}