Click here to Skip to main content
13,090,519 members (42,108 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have created table "Image" in access database that have 3 fields "ImageId","ImageName" and "Image_Path". I added below code to form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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")
        AddImageColumn(ds.Tables("Image"), "Image_stream")
        ' Loop to load the image for each row
        For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If Not String.IsNullOrEmpty(ds.Tables("Image").Rows(index).Item("Image_Path").ToString) Then
                'LoadImage(ds.Tables("Image").Rows(index), "Image", _
                LoadImage(ds.Tables("Image").Rows(index), "Image_stream", ds.Tables("Image").Rows(index).Item("Image_Path").ToString)
            End If
        '* End code to add
        rptDoc.ReportSource = ds.Tables("Image")
        ' Display report in a report viewer control
    End Sub
    Private Sub AddImageColumn(ByVal objDataTable As DataTable, ByVal strFieldName As String)
            Dim objDataColumn As DataColumn = New DataColumn(strFieldName, Type.GetType("System.Byte[]"))
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
    Private Sub LoadImage(ByVal objDataRow As DataRow, ByVal strImageField As String, ByVal FilePath As String)
            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))
            objDataRow(strImageField) = Image
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

But I get filenotfound error on line:
Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)

Although that path contains image.Can any1 help me please?
Posted 2-Jan-13 20:25pm

1 solution

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

Solution 1

if you have stored image directly in database not a path of image;

It is quite easy,

just pass data-source having image column (e.g. format is binary) and drag-drop field from datasource in crystal report as you do for a simple text field

Happy Coding!
Rachna0309 3-Jan-13 2:45am
But Access 2000 does not store image directly.
Aarti Meswania 3-Jan-13 2:47am
are you storing image path in database?
Rachna0309 3-Jan-13 2:51am
Aarti Meswania 3-Jan-13 2:56am
read data from database
now add an extra image-data-column to datatable, e.g. on link...

run a for loop inside that read path, fetch image save it in cell of datatable

now, create xml from that datatable
e.g. (dt.writexml(path...)

assign that xml to crystal report
and drag-drop image field

Rachna0309 3-Jan-13 3:00am
Can you please put down code?I am newbie
Aarti Meswania 3-Jan-13 3:07am
you have already do code for preparing datatable with image

now just write...
rptDoc.ReportSource = ds.Tables("Image")

execute once
check file 'a.xml' is created on location here it is E drive.

now, open report
datasource -> ADO -> choose this a.xml file -> finish and... refresh datasource now, you get that image column through xml
save report

and run project....
Rachna0309 3-Jan-13 3:24am
i am not able to add .xml file to datasource..
Aarti Meswania 3-Jan-13 3:31am
first do you get xml file... on location? like i create it in e drive.
Rachna0309 3-Jan-13 3:32am
yes i got it in my projects folder
Aarti Meswania 3-Jan-13 3:42am
open crystal report
see field explorer
right click database fields >> database expert >> create new datasouce >> >> now browse your a.xml file >> finish

now you are back to database expert window...
use << button to cancel existing datasouce and add that new one to right hand panel
ok... now you get that in report

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 |
Web01 | 2.8.170813.1 | Last Updated 3 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