Saturday, August 10, 2019

How to create Microsoft Report (.rdlc) with multiple datasources in ASP.NET

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table and insert data for show in report

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used two tables as below
Table 1
    Table 2

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add Dataset.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select Dataset under data > Enter Dataset name > Add.
Here you can add Datatable in 2 ways.
1. By Dragging Table from solution explorer to dataset.
2. Right Click on Dataset > Add Datatable. ( Than add columns by Right Click on Data table > Add > Column.)

[ View video for inner details steps ]


Step-6: Add Report file(.rdlc) and Design your report.

Add report folder to your project
Right Click on report folder > Add > New item > Select Report under Reporing > Enter report file name > Add.
Here we also have to add Datasource for our report.

Under report data Click on New > Dataset > Enter Dataset name > Select Datasource > Select Dataset > Ok.
(Repeat this step twice for add 2 data sources, that we have already created.) 
Now Design your Report looks.
Here I have added 2 table for show data of 2 Datasource.
For Create Table --> Right Click on Report Body > Insert > Table.
(Repeat this step twice for add 2 table) 

Here we have to do 1 more thing, that is have set DataSetname Name for this 2 table.
For Set DataSetname --> Select table > Go to Properties > Set DataSetname.
(Repeat this step twice for add 2 table) 
My Design Looks like this....


[ View video for inner details steps ]

Step-7: Add a Web page for Show Report in Report Viewer.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

Drag Control ScriptManager and ReportViewer to your page.

Html Code 
  1. <h2>
  2. Microsoft Report using multiple datasource in ASP.NET
  3. </h2>
  4. <div>
  5. <asp:ScriptManager ID="ScriptManager1" runat="server">
  6. </asp:ScriptManager>
  7. <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
  8. Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
  9. WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="857px">
  10. <LocalReport ReportPath="Report\TopTenList.rdlc">
  11. </LocalReport>
  12. </rsweb:ReportViewer>
  13. </div>
Write Below code in page_load event
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateReportData();
  6. }
  7.  
  8. }
And here is the function 
  1. private void PopulateReportData()
  2. {
  3. List<EverestList> allEverest = new List<EverestList>();
  4. List<RiverList> allRiver = new List<RiverList>();
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. allEverest = dc.EverestLists.ToList();
  8. allRiver = dc.RiverLists.ToList();
  9. }
  10. ReportViewer1.LocalReport.DataSources.Clear();
  11. ReportDataSource rd1 = new ReportDataSource("DSEverest", allEverest);
  12. ReportDataSource rd2 = new ReportDataSource("DSRiver", allRiver);
  13. ReportViewer1.LocalReport.DataSources.Add(rd1);
  14. ReportViewer1.LocalReport.DataSources.Add(rd2);
  15. ReportViewer1.LocalReport.Refresh();
  16. }
Step-8: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...