Click here to Skip to main content
15,562,414 members
Articles / Web Development / ASP.NET
Article
Posted 11 Nov 2008

Stats

211.9K views
53 bookmarked

How to dynamically load images in Crystal Reports using Visual Studio 2005

Rate me:
Please Sign up or sign in to vote.
4.55/5 (23 votes)
19 Nov 2008CPOL
How to dynamically load images in Crystal Reports in ASP.NET, using the TypedDataSet.

Introduction

If you have an image path that is stored in your database, Crystal Reports .NET in Visual Studio 2003/2005 cannot display the image file dynamically unless you use the dynamic image location feature in Crystal Report XI.

I searched in the Web, and I found how to display this using a workaround.

Using the code

We must have a field (image path) in our database. We need to create a new DataSet/XML schema (XSD) (TypedDataSet) to use as resource data in creating a report, and add an additional field that is not in the table and which is of type base64Binary:

Image 1

Or add it in XML:

XML
< name=""image_stream"" type=""xs:base64Binary"" minoccurs=""0″">

When designing a report, drag and drop the “image_stream” field in the region where you want it to appear. Add CrystalReportViewer to your ASPX page. In the code-behind of your page, add the following method to load the image:

C#
private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
{
    try
    {
        FileStream fs = new FileStream(FilePath, 
                   System.IO.FileMode.Open, System.IO.FileAccess.Read);
        byte[] Image = new byte[fs.Length];
        fs.Read(Image, 0, Convert.ToInt32(fs.Length));
        fs.Close();
        objDataRow[strImageField] = Image;
    }
    catch (Exception ex)
    {
        Response.Write("<font color=red>" + ex.Message + "</font>");
    }
}

We need to fill the TypedDataSet, and before assigning this DataSet to the “SetDataSource” of our report, we need to add a few lines of code:

C#
TypedDataSet ds = new TypedDataSet();

SqlConnection cn = new SqlConnection("Data Source=ServerName;" + 
                   "Initial Catalog=DataBaseName;User ID=UserName;" + 
                   "Password=UserPassWord");
SqlCommand Cmd = new SqlCommand();
SqlDataAdapter myAdapter = new SqlDataAdapter();

Cmd.CommandText = " Select * From TableName";
Cmd.Connection = cn;

myAdapter.SelectCommand = Cmd;

try
{
    cn.Open();
    myAdapter.Fill(ds.Tables[0]);
    cn.Close();
}
catch (Exception ex)
{
    throw;
}


for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
{
    if (ds.Tables[0].Rows[index]["image_path"].ToString() != "")
    {
        string s = this.Server.MapPath(
                    ds.Tables[0].Rows[index]["image_path"].ToString());

        if (File.Exists(s))
        {
            LoadImage(ds.Tables[0].Rows[index], "image_stream", s);
        }
        else
        {
            LoadImage(ds.Tables[0].Rows[index], "image_stream", 
                      "DefaultPicturePath");
        }
    }
    else
    {
        LoadImage(ds.Tables[0].Rows[index], "image_stream", 
                  "DefaultPicturePath");
    }
}

try
{
    // Finally display report in crystal report viewer
    ReportDocument crDoc = new ReportDocument();
    crDoc.Load(Server.MapPath("CrystalReport.rpt"));
    crDoc.SetDataSource(ds.Tables[0]);
    CrystalReportViewer1.ReportSource = crDoc;
}
catch (Exception ex)
{
    throw;
}

Thanks

I would like to thank the CodeProject team (moderators, users, visitors...) for all their effort. Thank you everybody.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Morocco Morocco
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
ArmandoHeck22-Jan-18 2:48
ArmandoHeck22-Jan-18 2:48 
QuestionResize images Pin
Alby20042-Mar-17 23:48
Alby20042-Mar-17 23:48 
QuestionThank you so much Pin
Member 124452768-Apr-16 4:18
Member 124452768-Apr-16 4:18 
QuestionPERFECT! Pin
Member 119651327-Sep-15 1:31
Member 119651327-Sep-15 1:31 
Questioncrystal report Pin
prabhu221022-Apr-15 3:09
prabhu221022-Apr-15 3:09 
QuestionColumn Type of image_stream should be image Pin
monishafriends21-Oct-14 22:49
monishafriends21-Oct-14 22:49 
QuestionPicture not repeated through all the pages Pin
Savun Cheam12-Nov-13 20:56
Savun Cheam12-Nov-13 20:56 
QuestionI have reduced the number of lines of code Pin
Prathap Gangireddy30-Jan-13 9:59
Prathap Gangireddy30-Jan-13 9:59 
GeneralThankyou So Much Pin
Member 222171713-Mar-12 16:40
Member 222171713-Mar-12 16:40 
GeneralMy vote of 5 Pin
ganpati122-Nov-11 0:13
ganpati122-Nov-11 0:13 
QuestionThank-You Redouane - Just what I required. Pin
razzleblazzle6-Oct-11 10:03
razzleblazzle6-Oct-11 10:03 
AnswerRe: Thank-You Redouane - Just what I required. Pin
Redouane Mounafia6-Oct-11 22:54
Redouane Mounafia6-Oct-11 22:54 
Questionimage is not showing Pin
mayur csharp G4-Oct-11 2:28
mayur csharp G4-Oct-11 2:28 
AnswerRe: image is not showing Pin
razzleblazzle6-Oct-11 10:09
razzleblazzle6-Oct-11 10:09 
Questionwhat send the image to Crystall Reports? Pin
y.mahdi21-Apr-11 21:38
y.mahdi21-Apr-11 21:38 
Answerhelp me, there's an exception Pin
Kenz Kenza14-Apr-11 17:19
Kenz Kenza14-Apr-11 17:19 
Generalpdf too large Pin
yi24-Oct-10 0:51
yi24-Oct-10 0:51 
GeneralFor desktop Pin
shailesh_j2-Dec-09 21:25
shailesh_j2-Dec-09 21:25 
GeneralRe: For desktop Pin
Redouane Mounafia3-Feb-10 6:18
Redouane Mounafia3-Feb-10 6:18 
GeneralThanks Pin
Wags6-Aug-09 23:25
professionalWags6-Aug-09 23:25 
GeneralRe: Thanks Pin
Redouane Mounafia7-Aug-09 9:30
Redouane Mounafia7-Aug-09 9:30 
GeneralNot working with VB.net 2003 Pin
sachinse14-Jun-09 22:11
sachinse14-Jun-09 22:11 
GeneralImage not showing Pin
dannystommen3-Apr-09 5:34
dannystommen3-Apr-09 5:34 
GeneralRe: Image not showing Pin
dannystommen7-Apr-09 0:12
dannystommen7-Apr-09 0:12 
GeneralI don't see any picture - Need help Pin
sharabi_liran2-Feb-09 21:29
sharabi_liran2-Feb-09 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.