Click here to Skip to main content
15,881,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a crystal report created in vb.net 2010. It displays images at runtime using image path stored in db table.
Certain images do not display at all even though file is present at the path,
whereas certain images do display properly.

can anybody tell me what could be the problem?

Following is the code that i have used to generate the image from img path stored in db.
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = DisplayHeaderText;
            SqlConnection cn = new SqlConnection("Data Source=" + RptServer + ";Initial Catalog=" + RptDatabase + ";user=" + RptUser + ";Password=" + RptPassword);
            SqlCommand Cmd = new SqlCommand();
            SqlDataAdapter myAdapter = new SqlDataAdapter();

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

            myAdapter.SelectCommand = Cmd;

            cn.Open();
            DataSet ds = new DataSet();
            myAdapter.Fill(ds);
            cn.Close();

            DataTable dttmp = ds.Tables[0];

            dttmp.Columns.Add("IsImage", System.Type.GetType("System.Byte[]"));

            for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
            {
                if (dttmp.Rows[index]["ImgFile"].ToString() != "")
                {
                    string s = dttmp.Rows[index]["ImgFile"].ToString();
                    LoadImage(dttmp.Rows[index], s);
                }
                else
                {
                    LoadImage(dttmp.Rows[index], "");
                }
            }

            // Finally display report in crystal report viewer
            ReportDocument crDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            string rptPath = Application.StartupPath;
            crDoc.Load(rptPath + "\\SalesWithImage.rpt");
            crDoc.SetDataSource(dttmp);
            crptViewer.ReportSource = crDoc;
            crDoc.DataDefinition.FormulaFields[0].Text = "'" + FltrHeaderText + "'";
        }




 private void LoadImage(DataRow objDataRow, string FilePath)
        {
            if (FilePath == "")
            {
                return;
            }

            if (File.Exists( FilePath))
            {
                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));
                
                BinaryReader br = new BinaryReader(fs); 
                byte[] imgbyte = new byte[fs.Length + 1]; 
                imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
                fs.Close();

                objDataRow["IsImage"] = imgbyte;
            }
        }
Posted
Updated 17-Oct-12 8:46am
v3
Comments
Thomas Daniels 17-Oct-12 13:58pm    
I've a small question.
How do you lock editing for Q&A?
Sandeep Mewara 17-Oct-12 14:57pm    
Nobody does that specifically. It automatically happens once someone is editing to avoid concurrent issue.
Thomas Daniels 18-Oct-12 11:13am    
Ok, thanks!
Sandeep Mewara 17-Oct-12 14:58pm    
Certain images do not display at all even though file is present at the path,
Any pattern?

When you DEBUG, do you see everything as is.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900