Click here to Skip to main content
12,763,299 members (37,707 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
I want to display image in crystal report from path given in database table.My code runs well but nothing is displayed in report.Below is my code.Please help me to find out the problem.
Dim Con As OdbcConnection = New OdbcConnection
        Dim sql As String = "Select * from Image"
        Con.ConnectionString = connstring
        Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, Con)
        da.Fill(ds, "Image")
        ds.Tables("Image").Columns.Add("image_stream", Type.GetType("System.Byte[]"))


        For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If ds.Tables("Image").Rows(index)("image_path").ToString <> "" Then
                Dim s As String = ds.Tables("Image").Rows(index)("image_path").ToString
                If File.Exists(s) Then
                    LoadImage(ds.Tables("Image").Rows(index), "image_stream", s)

                End If
            End If
        Next index

        crDoc = New ImageReport
        crDoc.SetDataSource(ds.Tables("Image"))
        rptDoc.ReportSource = crDoc



Private Sub LoadImage(ByVal objDataRow As DataRow, ByVal strImageField As String, ByVal FilePath As String)
        Try
            Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim Image() As Byte = New Byte(fs.Length) {}
            fs.Read(Image, 0, CType(fs.Length, Integer))
            fs.Close()
            objDataRow(strImageField) = Image
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
Posted 7-Jan-13 20:48pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

fill image in cell of data table
and make sure image is successfully read and inserted in to cell

For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If ds.Tables("Image").Rows(index)("image_path").ToString <> "" Then
                Dim s As String = ds.Tables("Image").Rows(index)("image_path").ToString
                If File.Exists(s) Then

Drawing.Bitmap img = new Drawing.Bitmap(ds.Tables("Image").Rows(index)("image_path").ToString); //Replace string with your OpenFileDialog path.

MemoryStream ms = new MemoryStream();
     Image returnImage = Image.FromStream(ms);
System.Drawing.Image imageIn = new System.Drawing.Image();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);

                  ds.Tables("Image").Rows(index)("image_stream") =  ms.ToArray(); 

                End If
            End If
        Next index

Happy Coding!
:)
  Permalink  
v3
Comments
Rachna0309 8-Jan-13 2:47am
   
In crystal report,which field should I use to display image?
Rachna0309 8-Jan-13 2:50am
   
Type of value has a mismatch with column typeCouldn't store <system.drawing.bitmap> in image_stream Column. Expected type is Byte[].

I get this error at line:
ds.Tables("Image").Rows(index)("image_stream") = img;
Aarti Meswania 8-Jan-13 2:53am
   
use datatype varbinary(Max) or image in database
eg. your query should be
select *,convert(image,null) as image_stream from tabelname

so it will get an empty image column in crystal report
and you will read pass value using newly prepared datatable runtime
Aarti Meswania 8-Jan-13 2:55am
   
see updated solution
Rachna0309 8-Jan-13 3:01am
   
Access 2003 does not have datatype varbinary or image.
Aarti Meswania 8-Jan-13 3:04am
   
may be you will have ole datatype there
I prefer you to use xml for this report it will not make such problem
Rachna0309 8-Jan-13 3:04am
   
Updated solution gives error of datatype.
Aarti Meswania 8-Jan-13 3:10am
   
then use system.bytes datatype and use updated solution

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100