Display images in the Report Viewer

Sathya4260
Posted by in Windows Forms category on for Beginner level | Points: 250 | Views : 37234 red flag

Display the images in the rdlc reports, where the images are retrived from the database.

Introduction

Most of us using rdlc reports to show the reports and sometimes we need to show up the images in the reports. Let's see the method to work out this, step by step.



Objective

Display the images (saved in the databse) in the report viewer



Using the code

We usually save the images of the Database and then retrieve it to use for our purpose.Let us fallow the same in the scenario, I have already explained how to upload the images to the Database in the below link

http://www.dotnetfunda.com/articles/article1100-upload-images-to-database-and-view-in-gridview-.aspx

We would save the image as image data type in the Data base, it is stored as an binary format, the following code snippet is used to fill the data table with data retrieved from the Database.

 

String connStr= "Data Source=Server;Initial catalog=Database;User ID=ID;Password=Password;";

using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = "SELECT * FROM EmpTable ORDER BY Empnames";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        conn.Open();
                        ad.Fill(table);
                        conn.Close();
                    }
                }

Now we should create an strong typed dataset, with the same column name and datatypes as per in the database

Ex:

 

Datatable
EmpName-System.string

EmpID-System.Int

Empphoto-System.Byte[]

Now (i) create an report viewer (ii) add datatable as the datasource and (iii) drop a Table from the toolbox

Add the columns as fallow

Empid,EmpName,EmpPhoto

In the data column of Empphoto just right click and select image properties

(i) select the image source as "Database"

(ii) select the column as "EmpPhoto"

(iii) select the MIME type as per the image you uploaded.

Following code snippet is meant for achieving the loadingof report in the report viewer.


string exeFolder = new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName;

string reportPath = Path.Combine(exeFolder, @"report.rdlc");

this.report.LocalReport.ReportPath = reportPath;

this.report.LocalReport.EnableExternalImages = true;

this.report.LocalReport.DataSources.Add(new ReportDataSource("DataTable",dataset.tables[0]);

this.report.RefreshReport();

 

Now compile and run application, our task is achieved.

Conclusion

The images (which are saved in the database) can be displayed in edlc reports with simple method as described above.

Reference

http://www.dotnetfunda.com/articles/article1100-upload-images-to-database-and-view-in-gridview-.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Sathya4260
Full Name: sathish kumar
Member Level:
Member Status: Member
Member Since: 12/17/2010 7:34:35 AM
Country: India
Sathish Kumar S

GNIIT Software Engineer, MCPD web Developer, MCTS .Net Framework and Sql server certified

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)