Click here to Skip to main content
Licence CPOL
First Posted 11 Nov 2008
Views 54,436
Bookmarked 38 times

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

By Redouane Mounafia | 19 Nov 2008
How to dynamically load images in Crystal Reports in ASP.NET, using the TypedDataSet.
1 vote, 7.7%
1

2

3
3 votes, 23.1%
4
9 votes, 69.2%
5
4.62/5 - 13 votes
1 removed
μ 4.19, σa 2.04 [?]

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:

Or add it in 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:

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:

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)

About the Author

Redouane Mounafia

Team Leader

Morocco Morocco

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberganpati10:13 22 Nov '11  
QuestionThank-You Redouane - Just what I required. Pinmemberrazzleblazzle10:03 6 Oct '11  
AnswerRe: Thank-You Redouane - Just what I required. PinmemberRedouane Mounafia22:54 6 Oct '11  
Questionimage is not showing Pinmembermayur csharp G2:28 4 Oct '11  
AnswerRe: image is not showing Pinmemberrazzleblazzle10:09 6 Oct '11  
Questionwhat send the image to Crystall Reports? Pinmemberabbass0912873321:38 21 Apr '11  
Answerhelp me, there's an exception Pinmembersarrenza17:19 14 Apr '11  
Generalpdf too large Pinmemberyi20:51 4 Oct '10  
GeneralFor desktop Pinmembershailesh_j21:25 2 Dec '09  
GeneralRe: For desktop PinmemberRedouane Mounafia6:18 3 Feb '10  
GeneralThanks PinmemberWags23:25 6 Aug '09  
Your article saved me loads of time. I had given up on trying to get Crystal Reports to display data in a chart that I could configure the way that I required. Now I just create the chart using ChartFX and export it as a bitmap to the report. Smile | :)
 
"...there's what people want to hear, there's what people want to believe, there's everything else, THEN there's the truth!" - New York D.A., The International

GeneralRe: Thanks PinmemberRedouane Mounafia9:30 7 Aug '09  
GeneralNot working with VB.net 2003 Pinmembersachinse22:11 14 Jun '09  
GeneralImage not showing Pinmemberdannystommen5:34 3 Apr '09  
GeneralRe: Image not showing Pinmemberdannystommen0:12 7 Apr '09  
GeneralI don't see any picture - Need help Pinmembersharabi_liran21:29 2 Feb '09  
QuestionThanks & Need your help PinmemberRajeeshun11:43 12 Jan '09  
AnswerRe: Thanks & Need your help PinmemberRedouane Mounafia13:09 12 Jan '09  
GeneralThanks! A note... PinmemberDigambar Kandangire4:25 27 Nov '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 19 Nov 2008
Article Copyright 2008 by Redouane Mounafia
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid