Search code examples
asp.net-mvcrdlc

Display Image from folder while the path is in Database In RDLC Report


How to Show Pictures of students which is located in folder and the path is in database But I'm unable to Do it I Searched a lot but found nothing useful. Please help me guys

enter image description here I am getting like this

            protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string query = @"select StudentID,c.DocumentType as DocumentID,DocumentNumber,Name,NameDari,FatherName,FatherNameDari,MobileNumber,Photo,b.ClassName as ClassID,d.LocationName as LocationID,e.TeacherName as TeacherID,a.Term,a.Score from StudentsInfo a 
join Class b 
on a.ClassID=b.ClassID
join Document c
on c.DocumentID=a.DocumentID
join Location d
on d.LocationID=a.LocationID
join Teachers e
on e.TeacherID=a.TeacherID";
                var data = db.Database.SqlQuery<StudentsInfo>(query);
                ReportViewer1.SizeToReportContent = true;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("Certificates.rdlc");

                ReportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource ds = new ReportDataSource("Certificates", data);
                ReportViewer1.LocalReport.DataSources.Add(ds);
                this.ReportViewer1.LocalReport.EnableExternalImages = true;
                ReportViewer1.LocalReport.Refresh();
            }

        }

Solution

  • First, make sure that you're already done these steps in RDLC report designer:

    1) Set "External" mode from image source property.

    2) Create a report parameter to hold inserted path from code behind by using Add Parameter from context menu.

    Then, you can try example below after enabling EnableExternalImages:

    this.ReportViewer1.LocalReport.EnableExternalImages = true;
    
    /* begin added part */
    
    // get absolute path to Project folder
    string path = new Uri(Server.MapPath("~/path/to/Project/folder")).AbsoluteUri; // adjust path to Project folder here
    
    // set above path to report parameter
    var parameter = new ReportParameter[1];
    parameter[0] = new ReportParameter("ImagePath", path); // adjust parameter name here
    ReportViewer1.LocalReport.SetParameters(parameter);
    /* end of added part */
    
    ReportViewer1.LocalReport.Refresh();
    

    Then in report expression editor, define simple expression that concatenates inserted path from ReportParameter and stored path from database (here the parameter name should match with ReportParameter in example code above):

    =Parameters!ImagePath.Value + Fields!Photo.Value
    

    References: