In this article I will explain with an example, how to pass Parameter to RDLC Report programmatically from Code Behind in ASP.Net using C# and VB.Net.
The Parameters will be passed to RDLC Report using object of the ReportParameter class in ASP.Net using C# and VB.Net.
This is applicable to all ReportViewer controls belonging to all different Visual Studio versions such as 2010, 2012, 2013, 2015, 2017 and 2019.
Configuring the RDLC Report
1. I have already added a blank RDLC Report and an Image to the Solution folder.
2. Now add a Parameter to the RDLC Report of type text and set its name.
3. Insert an Image on to the RDLC Report.
Set its name, ToolTip to be displayed and make sure you select the Image Source as External. Next we need to set the following formula which means that path of the image will be fetched from the ImagePath Report Parameter we have added in step #2.
=Parameters!ImagePath.Value
Then in the Size Tab you need to set the Display property as Original Size.
Once image gets added you should see as below.
HTML Markup
The HTML Markup consists of ASP.Net ScriptManager and ReportViewer control.
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server">
</rsweb:ReportViewer>
</form>
</body>
</html>
Namespaces
You will need to import the following namespace.
C#
using Microsoft.Reporting.WebForms;
VB.Net
Imports Microsoft.Reporting.WebForms
Pass Parameter to RDLC Report from Code Behind in ASP.Net
In the below code, first thing I am setting the Report Path and then the most important property EnableExternalImages has to be set to True, otherwise the image will not be shown.
Finally we need to convert the image path to File Uri and then pass as value to the ImagePath Report Parameter and pass it to the RDLC Report.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
ReportViewer1.LocalReport.EnableExternalImages = true;
string imagePath = new Uri(Server.MapPath("~/images/Mudassar.jpg")).AbsoluteUri;
ReportParameter parameter = new ReportParameter("ImagePath", imagePath);
ReportViewer1.LocalReport.SetParameters(parameter);
ReportViewer1.LocalReport.Refresh();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
ReportViewer1.LocalReport.EnableExternalImages = True
Dim imagePath As String = New Uri(Server.MapPath("~/images/Mudassar.jpg")).AbsoluteUri
Dim parameter As New ReportParameter("ImagePath", imagePath)
ReportViewer1.LocalReport.SetParameters(parameter)
ReportViewer1.LocalReport.Refresh()
End If
End Sub
Once you execute the application you should be able to view the external image in RDLC Report as shown below.
Image not visible and Cross Image appears in RDLC Report
Now in spite of following all steps if you still do not see the report and you see a cross image icon on the RDLC Report, then you need to follow the following procedure.
You will need to make sure that either both or one of the following Handlers are added to the Web.Config file.
1. Add the following to <system.web> => <httpHandlers> section.
<add verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
2. Add the following to <system.webServer> => <handlers> section.
<add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Note: If you don’t have both sections in your Web.Config then you can add only to the one you have.
Demo
Downloads