Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Dynamically display images in crystal report from database in asp.net

Feb 10, 2012
Introduction:

In this article I will explain how to show images in crystal reports from database in asp.net.


Description:

In Previous posts I explained many articles relating to
crystal reports. Now in this post I will explain how to dynamically display images in crystal reports from database in asp.net.

To implement this concept first we need to design the table in database and give name as Image

Column Name
Data Type
Allow Nulls
ImageId
Int(set identity property=true)
No
ImageName
Varchar(50)
Yes
Image
image
Yes

After completion of table design insert images data in table by using this post insert images in database using asp.net. After insertion of images in database follow below steps to display images in crystal reports using asp.net.

Now Open visual studio and create new website after that right click on your website and select Add new item in that select Crystal Report and click Add
 
After that add crystal report then it will prompt Crystal Report Gallery window in that select blank solution and click OK

A blank report will create in our application now click on CrystalReports menu under that select Database under that select Database Expert
 
After click on Database Expert now Database Expert wizard will open in that select Create New Section >> select OLE DB (ADO) >> in that click on + sign of OLE DB (ADO)

Now select Microsoft OLE DB Provider for SQL Server and click Next (Here we can select SQL Native client option also but sometimes during deployment if servers not contains this native client it will throw error).
 
Now enter SQL Server name, username, password and required database and click Next

After enter credentials for your required database click Next then click Finish (Here for my database I didn’t set any credentials for that reason I didn’t enter userid and password details don’t get confused).

After click Finish now our database loaded in OLEDB (ADO) section >> select your database >> select dbo >> select required tables

Now open tables in that select required table and move to selected tables section and click OK

After that Database Fields in Field Explorer populated with our required data table now drag and drop the required fields from data table to reports Details section

Now open your Default.aspx page drag and drop CrystalReportViewer control from Reporting tab.


Now select CrystalReportViewer and click on smart tag in right hand side and Choose new Report Source

Whenever we click on New report source one window will open in that select crystal report for Report Source from the available reports in dropdownlist and click OK.

After assign available report to CrystalReportViewer control check your code that would be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now run your application your report will be like this

In case your report prompt window for UserName and password before we access data in that situation we need to set those details in code behind instead of assign crystal report to CrystalReportViewer control

Drag and drop CrystalReportViewer control click on right side smart tag of your CrystalReportViewer control and uncheck EnableDatabaseLogonPrompt

Our aspx code will be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now Open your code behind file and set database connection settings and assign reports to the control before that first add following namespaces


using System;
using CrystalDecisions.CrystalReports.Engine;
After add namespaces write the following code in page load event

C# code


protected void Page_Load(object sender, EventArgs e)
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("CrystalReport.rpt"));
reportdocument.SetDatabaseLogon("username","password","SureshDasari","MySampleDB");
CrystalReportViewer1.ReportSource = reportdocument;
}
VB.NET Code


Imports CrystalDecisions.CrystalReports.Engine

Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim reportdocument As New ReportDocument()
reportdocument.Load(Server.MapPath("CrystalReport.rpt"))
reportdocument.SetDatabaseLogon("", "", "SureshDasari", "MySampleDB")
CrystalReportViewer1.ReportSource = reportdocument
End Sub
End Class
Now run your application and check your output that would be like this


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

29 comments :

Anonymous said...

Hi suresh..excellent tutorial..how can i show this blob image field page back side for each customer?(contains more than one image for each customer)

Murali &Jayachandran said...

thank you so much for clear explanation...its really amazing ur service

Anonymous said...

nice one....

rajesh said...

hi suresh this is nice tutorial.
can we display barcode in report?
thanks

Anonymous said...

can You show Use of line Graph...
with for each Record and two tables

Anonymous said...

i hav to show dynamic image from DB..Can u help me??

Anonymous said...

thanks suresh your code help me a lot...

Anonymous said...

Excellent Article.Thanks a lot for detail explanation.

Neha630 said...

Can I Change The Query for Select Some Images.
Means..Can I add "Where" Clause in Sql Query through Coding.

Viral said...

what to do if image in BLOB format

Anonymous said...

Thanx sir...................):
for giving this information for me

Unknown said...

Hi how to print images in varies width and height in same page.

Bhavik said...

i think this is worth case to store image in database..
always store image in specific folder with unique name

Anonymous said...

My database have image name and images are in folder.so how can i display image on crystal report.Please help me.

asp net training said...

hi suresh, this is sufficient docs to understand crystal report. whenever we are provide asp net training.

Web Designing Company in Jaipur said...

I cannot thank you enough for the post.Really looking forward to read more.

Anonymous said...

I need to display image from URL saved in Table's column. i.e. ~/images/Pic1.jpg

Anonymous said...

Great post. But i need to display 4 images on 1 page i.e. matrix. can u please help me in this regard?

mailto: fahimahmedansari@gmail.com

Anonymous said...

Hi Suresh,

I need help i need to create a hyper link on image on clicking i want to display another the paticular image with its details. please help

Unknown said...

How can i know my Sql server Name?

Unknown said...

Thank You So Much Suresh

Anonymous said...

AOA :

How can Use crystal report on server, crystal report Run Time Environment.My web Deployed on server but Reports not work correctly

Deepak Pareek said...

Nice Blog Suresh Sir..!! Helps me a lot..!!Thanxx..! I m fan of u sir ji,,!!

PRATHAMESH SHAH said...

I have installed Cristal Report for VS 2010, but not allow me to add new Cristal Report.

Mamun said...

I am looking for this type of article.It's very helpful and appreciable.

Anonymous said...

thanks you show example was helpful for me

Unknown said...

can anyone tell me how to match contents of two mdb file using c#? Thanks in Advance

Unknown said...

how to display image dynamically from folder using filepath from database in crystal report 13 ?

Unknown said...

can we set full background image for crystal report???? ....using css code

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.