Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
VB
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", _
                'ds.Tables("Image").Rows(index).Item("Image_Path").ToString)
                LoadImage(ds.Tables("Image").Rows(index), "Image_stream", ds.Tables("Image").Rows(index).Item("Image_Path").ToString)

            Else
                
            End If
        Next
        '* 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)
        Try
            Dim objDataColumn As DataColumn = New DataColumn(strFieldName, Type.GetType("System.Byte[]"))
            objDataTable.Columns.Add(objDataColumn)
        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)
        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


But I get filenotfound error on line:
VB
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

1 solution

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

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!
:)
 
Share this answer
 
Comments
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    
yes.
Aarti Meswania 3-Jan-13 2:56am    
read data from database
now add an extra image-data-column to datatable, e.g. on link...
http://stackoverflow.com/questions/905419/image-in-datatable

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

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