Display image in crystal report from database in ASP.NET

No.of Views13100
Bookmarked1 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : ASP.NET , How to
Display image in crystal report from database in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Today I saw in CodeGain message board one of the user asked a question, how to display image in crystal report from the SQL Server 2005 in asp.net and he save image as binary data using file upload control asp.net.

Technologies

ASP.NET

Language

C# and T-SQL

Prerequisite

Visual Studio 2008, SQL Server 2005

Implementation

First we need to design a database to store image as binary format. For this we have to create table like follow.

Image Loading

Now we have database to store images.

Note: we assume have save few image in database already.

Let us start to retrieve image and display in crystal report. On this progress first steps is need design report.

Create new web application using Visual Studio 2008 and give a project name as "DisplayImageInCry" and then right click on the project and then select add new item.

Then select crystal report in template collection. Look at the following figure.

Image Loading

Then click OK button to continue the report design. When you click ok you will get report design wizard. Look following figure.

Image Loading

In this we need to select the using the report wizard option to continue and then click OK button to continue.

Image Loading

This screen is Important to move the next step. You know why we need to create database connection to design report. In above figure new need to select the "Create New Connection" node to create new connection.

 

Note: this is demonstration using SQL Server 2005. Here we need select SQL Native Client driver to create connection.

Click next button to specify database connection properties. It is look like figure below.

Image Loading

Select the table according to use in report and select column to build report. Finally it will look as following figure.

Image Loading

Note: its display first image from the database in the design time. But need write code to display image in dynamic way.

Let us start to write code. Here we need write code for read image from the database.

public DataSet ReadImageFromDB(int imageID){DataSet imageData = new DataSet();using (SqlConnection connection = new SqlConnection("connectionstring")){using (SqlCommand cmd = connection.CreateCommand()){cmd.CommandText = "select ImageData FROM UserImages where ID=" + imageID + ";";cmd.CommandType = System.Data.CommandType.Text;using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)){adapter.Fill(imageData);}}}return imageData;}

Above method code help us to retrive image from the database and fill into the dataset.

Next step is design a web page to render and display report in page. To this we need to add crystal report viewer in the default webpage.

When we add viewer in page should be similar like followings html tags.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CryReportWithImage.aspx.cs"
Inherits="Crystal_CryReportWithImage" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

Now we need to write code to display crystal report and display image.

using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using CrystalDecisions.CrystalReports.Engine;using CG.CS.Data.Sql;using System.Data;public partial class Crystal_CryReportWithImage : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){ReportDocument doc = new ReportDocument();doc.Load(Server.MapPath("ImageRpt.rpt"));ImageUtilityDA imageUtilityDA = new ImageUtilityDA();DataSet data = imageUtilityDA.ReadImageFromDB(131);doc.SetDataSource(data.Tables[0]);CrystalReportViewer1.ReportSource = doc;}}

Cool everything finished now build and run application.

Output look like followings

Image Loading

Note:

1. Here I didn't use any image resizing operation

2. For demonstration purpose we are displaying one image only. You can display multiple images.

3. Image is already saving to the database as binary format. 

Conclusion

This article explains about display image in crystal report from the Sql Server 2005 database Here i haven't written code for handle the image resize or formatting.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:tanmoyDate Of Posted:11/22/2010 2:19:24 AM
tanmoy
its very useful tips. thanks..
By:RRaveenDate Of Posted:5/31/2010 8:36:21 AM
Namespace
Hi, The CG.CS.Data.Sql is my own namespce name to dataaccess code of the "ReadImageFromDB" this method, so you just add your namespace name. instead to that.
By:Display image in crystal reporDate Of Posted:5/31/2010 7:09:08 AM
Display image in crystal report from database in ASP.NET
I have error in namespace using CG.CS.Data.Sql; please Rectifier that error and send me to mail. as early as possible
By:Display image in crystal reporDate Of Posted:5/31/2010 7:06:24 AM
Display image in crystal report from database in ASP.NET
I have error in namespace using using CG.CS.Data.Sql; Rectifier that error...
By:RRaveenDate Of Posted:5/29/2010 4:06:07 AM
The Same Way
Hi Meena, You can use the same idea, what i have used in asp.net web application.becauase both are same idea, but just you change the image folder or byte of the image from database and set to crystal report picture control. if still you are unable to do, please post the question in messageboard, we will give the sample code to there. thank you
By:MeenaDate Of Posted:5/29/2010 1:48:36 AM
How to display image in crystal report from database in only C#.Net(Windows Application)
How to display image in Crystal report from database in windows Application.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top