Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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!
Smile | :)
  Permalink  
v3
Comments
Rachna0309 at 8-Jan-13 2:47am
   
In crystal report,which field should I use to display image?
Aarti Meswania at 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
Rachna0309 at 8-Jan-13 3:01am
   
Access 2003 does not have datatype varbinary or image.
Aarti Meswania at 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 at 8-Jan-13 2:50am
   
Type of value has a mismatch with column typeCouldn't store in image_stream Column. Expected type is Byte[].
 
I get this error at line:
ds.Tables("Image").Rows(index)("image_stream") = img;
Aarti Meswania at 8-Jan-13 2:55am
   
see updated solution
Rachna0309 at 8-Jan-13 3:04am
   
Updated solution gives error of datatype.
Aarti Meswania at 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
0 OriginalGriff 645
1 Maciej Los 225
2 Abhinav S 171
3 Richard Deeming 160
4 Suvendu Shekhar Giri 159
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 9,135
2 Peter Leow 5,042
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,581


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2015
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