How to display images from SQL Server to Crystal Report?
In this article I have explained about how to display stored images from SQL Server table “image” field to Crystal Report. Here in this example I have connected crystal report with XML Dataset fields.
I have “Student” table like two fields sname and simage.
Description:
In my previous article I had explained about how to store images into SQL Server table "image" field.
http://dotnetspider.com/resources/42817-How-Upload-Image-SQL-Server.aspx
Now I have explained about how to display that images from SQL server table to Crystal Report.
How to create XML dataset with SQL Server table details?
Create dataset: Right Click your project name -> ADD new Item ->select data set (DataSet1.xsd)
Then right click of that dataset and add new column (for your field names, types) as same like in the SQL Server table field names.
How to change XML Dataset field as image?
After creating XML dataset fields then right click of the "simage" field in the XML dataset and choose properties -> change property as Byte "System.Byte[]".
How to connect dataset and CrystalReport Page in ASP.Net?
Right Click your project name -> ADD new Item ->Choose Crystal report and put name (sample name like this CrystalReport1.rpt).
Choose Field explorer in Left side and right click database Field -> Choose Database expert->choose new ADO.Net connection in tree and choose your dataset path and click finish.
Then your fields are displayed in field explorer drag and drop in to crystal report.
Note: If you change property as image in XML data set then only images are displayed in crystal report otherwise not displayed.
How to bind records in crystal report?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
sname = Request.QueryString["sname"];
try
{
sqlcon.Open();
if (sname == null || sname=="")
{
query = "select * from student";
}
else
{
query = "select * from student where sname='" + sname + "'";
}
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ReportDocument RptDoc = new ReportDocument();
RptDoc.Load(Server.MapPath("~/CrystalReport.rpt"));
RptDoc.SetDataSource(dt);
CrystalReportViewer1.ReportSource = RptDoc;
CrystalReportViewer1.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
sqlcon.Close();
}
}
}
Source Code Detail:
Here with I have attached source code with Database and its details. Download it and try to learn display images in crystal report.
Front End : ASP.NET
Code Behind : C#
Conclusion:
I hope my two articles is help to beginners for understand Concept of Image store and Retrieve/display process.