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