Monday, March 16, 2009

External Images in .rdlc Reports : ASP.NET

In this example I will explain how we can use external images in .rdlc reports.

Create a Web Application.

Right click on Project in Solution Explorer –> Add New Item – Select a .rdlc Report

image

Now Drag and Drop a image control in Header section in the Report.rdlc

Go to Report Menu in Visual Studio and Select Report Parameters –> and Add a parameter with name Path

image

Now we will set this path parameter to our image’s value property and provide the value at run time, select image and properties(F4) and set following values to Source and Value properties

Source = External and Value = !Path.value

image

Now create a Image folder in your solution and Add some images to that folder.

As I am only concentration on showing images on the report in this example I am not loading any data on to the report.

Now open Default.aspx page and add MicrosoftReportViewer Control and set MicrosoftReportViewer control source to Report1.rdlc

image

Now we are ready with design part,

One of the most important thing while displaying external images is to set EnableExternalImages perperty of MicrosoftReportViewer.LocalReport’s to True, this will allow us to show external images on the .rdlc reports

And also we need to set the Path parameter value so that image will be displayed on the report for that we need to all following code to Page_Load event

image 

Now we are ready with our code, just build and run and test

image

Hope this helps.

Tags : .rdlc report, external Images, ASP.NET

23 comments:

  1. Thanks for this article it solved one of my problems.

    Perhaps you might be able to help me with my other problem. How would I go about loading an unspecified amount of external images into a report from a folder on the web server?

    ReplyDelete
  2. You can add images dynamically to Report View control not to the Local Report following code will add image to report viewer
    img.ImageUrl = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "")
    + @"/Images/Report.jpg";
    ReportViewer1.Page.Header.Controls.Add(img);

    please let me know if it helped you.

    ReplyDelete
  3. Hi,

    I have tried the code but i am getting the following error.

    The Hidden expression used in image ‘image1’ returned a data type that is not valid.

    Please let me know what i am missing.

    ReplyDelete
  4. Actually I have created img as Image object which is System.Web.UI.WebControl.Image type please find the code which might help you, I didnt add any control at design time here I am creating control also at run time.

    Image img = new Image();
    img.ImageUrl = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "")
    + @"/Images/Report.jpg";
    ReportViewer1.Page.Header.Controls.Add(img);

    Regards,
    satish.

    ReplyDelete
  5. Gracias por este blog, tu explicación es muy buena... (gmmarcos@hotmail.com)

    Saludos!

    ReplyDelete
  6. Thanks Marcos, I didnt understand Spanish, however I had converted into English using Google Converter.

    Thanks for your appreciation.

    Regards,
    Satish.

    ReplyDelete
  7. Hi,
    I need a help in binding object to a .rdlc file.
    Actually My object contains one more object inside and a list of anothere object.
    Normal propertis of ihe object i can be able to bind and show in .rdl file.but the object and collection i cant be able to bind
    Need some assistance on that

    ReplyDelete
  8. Stis this also not dispaly image. n if display then it is not coming in pdf formate. so....... plz help

    ReplyDelete
  9. u can set image as Embeded go throught following
    Right click on Project in Solution Explorer –> Add New Item – Select a .rdlc Report
    then take image from toolbox then
    Go to Report Menu in Visual Studio and Select -->Embedded Image then select image whatever u want then give o.k
    then press f4 and give Source -- Embedded and
    in value pramater select image name which u give in Embedded image. then u get image in your report

    ReplyDelete
  10. How to get this if it is for windows application

    ReplyDelete
  11. hi,i am new to this blogs,i have developed RDLC Reports,
    i have developed Charts type is line,age on x axis and weight on y-aixs we have one age column and many weight columns,reports is completed,i want to show one text lik (p1 weigh)like for all weight,how i can show one fixed text on line,i think iots on like Datapoint,how i show i text on one line,different text on different lines,or how i show one text one data point in one line,by using datapoint it can repeated,cann i show data point once only in rdlc reports

    if any one knows pls send your reply to plhk26@gmail.com

    ReplyDelete
  12. Hello,

    I am new to these blogs. I am editing a report creating by a third party. It seems like the header of the report that has a table in the body has an embedded image as the table header. I've added some new items in the table and now need to edit this header to add the column name. I am unable to locate this image in the project. It has the following properties:
    MimeType = image/jpeg
    source = embedded
    name = report1Header

    Any ideas how I can accomplish this?

    Thanks.

    ReplyDelete
  13. Hi. I have did the following code but it is showing a broken image icon. the image i wanted to display is not showing.

    my code is to generate the report and image when user click on the button. I have put the code under the button click part.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. I have written the below code in VB.Net.
    Image is not shown on the report control

    Dim rds As New ReportDataSource("Reports", DataSetDS)
    _newObjReport.LocalReport.DataSources.Clear()
    _newObjReport.LocalReport.DataSources.Add(rds)

    PlaceHolderReport.Controls.Add(_newObjReport)

    _newObjReport.LocalReport.EnableExternalImages = True
    Dim param1 As List(Of ReportParameter) = New List(Of ReportParameter)

    param1.Add(New ReportParameter("HeaderImagepath", "_folder1/images/image.jpg"))
    _newObjReport.LocalReport.SetParameters(param1)

    _newObjReport.LocalReport.Refresh()

    please suggest.

    Thanking you.

    ReplyDelete
  16. Firstly, you take a new Form in your project on Load event you Wright this line below: reportViewer1.LocalReport.EnableExternalImages = true; after that take reportViewer on that page and set smart tag of that, choose Design a new report and take an image control on it from ToolBox, set its property Source = External Value = file:\D:Images\Sunset.jpg Note: Image(Sunset.jpg) saved in Images folder on D drive. You changed it according to your requirement.

    ReplyDelete
  17. Hi,I have tried given steps,getting "The Value expression for the image ‘image1’ is invalid (it is an empty string)." error,Please suggest

    ReplyDelete
    Replies
    1. you double click the paramater then clikc ok

      Delete
  18. Hi i have also tried given step bt image is not displaying and my code is

    Please suggest the essues in my code

    con = new SqlConnection(strCon);
    cmd = new SqlCommand("rdlsTest", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@RID", RqstID);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    ReportDataSource[] rds = new ReportDataSource[3];

    rds[0] = new ReportDataSource("DataSet1_rdlsTest", ds.Tables[0]);

    ReportDataSource rds1 = new ReportDataSource("DataSet1_DataTable1", ds.Tables[0]);
    ReportViewer1.LocalReport.DataSources.Clear();
    ReportViewer1.LocalReport.DataSources.Add(rds1);

    ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";

    ReportViewer1.LocalReport.EnableExternalImages = true;

    ReportParameter[] parameters = new ReportParameter[1];
    List param1 = new List();
    param1.Add(new ReportParameter("Path",Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,"")+@"/Images/gov.gif"));
    ReportViewer1.LocalReport.SetParameters(param1);


    ReportViewer1.LocalReport.Refresh();

    ReplyDelete
  19. sir image not showing there a "X" Printed what i do

    ReplyDelete
  20. Command
    string path = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,"")+@"/Images/gov.gif";
    is very nice.
    If You have AbsoluteUri with parameters, (e.g. http://localhost:56595/Default.aspx?hqsdf=znstryh5ki2ydjo2oobesqr3 ), You are with this command in hell.
    Imho better is this variant:
    string path = Request.Url.AbsoluteUri.Substring(0,Request.Url.AbsoluteUri.IndexOf('?')).Replace(Request.Url.AbsolutePath,"") + @"/Images/gov.gif";

    ReplyDelete