RDLC Report in ASP.Net MVC Razor/.Aspx

This article explains RDLC reports in MVC razor or .aspx.

Step 1: Create new folder as Report or other.

Step 2: If you have used a .cshtml page (Razor) then you also need to add a .Aspx page inside the folder (Report) as in above.

Step 3:
.cshtml is where you need to show the report; add the following code:

  1. <div id="autoUpdate" style="display: none; overflow-y: auto" class="SlideContainer">  
  2.     <table width="100%" height="100%">  
  3.         <tr>  
  4.             <td>  
  5.                 <table>  
  6.                     <tr>  
  7.                         <td></td>  
  8.                         <td></td>  
  9.                     </tr>  
  10.                 </table>  
  11.             </td>  
  12.         </tr>  
  13.         <tr><td><iframe id="myReport" width="100%" height="800px"></iframe></td></tr>  
  14.     </table>  
  15. </div> 

And add a JavaScript code to the page:

  1. $(window).load(function () {  
  2.     document.getElementById("myReport").src = "../../Report/CommonrdlcReport.aspx?ReportID=" + firstDropVal + "";  
  3.     $("#autoUpdate").show();  
  4. }); 

In the JavaScript code above a parameter; if you don't want t then just remove this: + firstDropVal + ""; and a name ie.DEMO in the query string.

Step 4: Now add a DataSet from Add New item from Visual Studio and add it under the Report folder or root folder.

Step 5: Now create a procedure in SQL and in the Dataset UI right-click and select "Add TableAdopter" and use an existing procedure so you can access it in the RDLC further.

Step 6: Now create a RDLC folder in the root and add Report.rdlc from both. You can select whichever you want. Then to open the RDLC go to Dataset, add Datasets and select the Data source. If you are doing this the first time then click on "New" and access the procedure or table that you created. Then from the Available datasets select a record and click "OK".

Step 7: If you want to show a report then do that like this:

Show report

Add a table to the RDLC and create a parent group and right-click on the parent group field then insert a column add inside the group right-click and add another column field then right-click on the second field then select TextBox properties then select Visibility then select Hide and below select the Parent group field so you can see like above.

Step 8: Now go to the Report folder and select the .aspx page then go to the code behind or just double click. Now for the Page Load write the following code:

  1. if (!Page.IsPostBack)  
  2. {  
  3.     if (Request.QueryString["ReportID"] !=null)  
  4.     {  
  5.     }  
  6.     SqlCommand cmdLatest = OpenConnection("SlideSearchDetails");  
  7.     cmdLatest.Parameters.Add("@UserAlias", reportID.Trim());  
  8.     cmdLatest.Parameters.Add("@StartDate", StartDate.Trim());  
  9.     cmdLatest.Parameters.Add("@EndDate", EndDate.Trim());  
  10.     DataSet dt = GetDataSet(cmdLatest);  
  11.     ReportViewer1.Reset();  
  12.     ReportViewer1.LocalReport.ReportPath = "RDLC/rptSearchKeyWord.rdlc";  
  13.     ReportViewer1.LocalReport.DataSources.Clear();  
  14.     ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("SlideSearchkey", dt.Tables[0]));  
  15. } 

Outside of the page load make a function as in the following:

  1. public SqlCommand OpenConnection(string strSpName)  
  2. {  
  3.     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["slideConnectionString"].ConnectionString);  
  4.     SqlCommand cmd = new SqlCommand();  
  5.     cmd.CommandText = strSpName;  
  6.     cmd.Connection = con;  
  7.     cmd.CommandType = CommandType.StoredProcedure;  
  8.     con.Open();  
  9.     return cmd;  
  10. }  
  11. public DataSet GetDataSet(SqlCommand cmd)  
  12. {  
  13.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  14.     DataSet ds = new DataSet();  
  15.     da.Fill(ds);  
  16.     return ds;  
  17. } 

If you are using a parameter then you need to use the following code.

Here I am using an image as a parameter.

  1. string DomainURL = Request.Url.GetLeftPart(UriPartial.Authority);  
  2. ReportViewer1.Reset();  
  3. List<ReportParameter> reportParameter = null;  
  4. reportParameter = new List<ReportParameter>(1);  
  5. reportParameter.Add(new ReportParameter("SlideURL", DomainURL));  
  6. ReportViewer1.LocalReport.ReportPath = "RDLC/rptReport1.rdlc";  
  7. ReportViewer1.LocalReport.EnableExternalImages = true;  
  8. ReportViewer1.LocalReport.SetParameters(reportParameter);  
  9. ReportViewer1.LocalReport.DataSources.Clear();  
  10. ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", dt.Tables[0])); 

If you use an image in RDLC then add the image and select properties then select the image source as External and below that write an expression for the field you need to show the image in as in the following:
 
=Parameters!SlideURL.Value & Fields!ThumnailPath.Value

RDLC 


Similar Articles