Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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", _
                '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:
 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
good
Please Sign up or sign in to vote.

Solution 1

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!
Smile | :)
  Permalink  
Comments
Rachna0309 at 3-Jan-13 2:45am
   
But Access 2000 does not store image directly.
Aarti Meswania at 3-Jan-13 2:47am
   
are you storing image path in database?
Rachna0309 at 3-Jan-13 2:51am
   
yes.
Aarti Meswania at 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 at 3-Jan-13 3:00am
   
Can you please put down code?I am newbie
Aarti Meswania at 3-Jan-13 3:07am
   
you have already do code for preparing datatable with image now just write... ds.Tables("Image").writexml("E:\a.xml") 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 at 3-Jan-13 3:24am
   
i am not able to add .xml file to datasource..
Aarti Meswania at 3-Jan-13 3:31am
   
first do you get xml file... on location? like i create it in e drive.
Rachna0309 at 3-Jan-13 3:32am
   
yes i got it in my projects folder
Aarti Meswania at 3-Jan-13 3:42am
   
open crystal report see field explorer right click database fields >> database expert >> create new datasouce >> ado.net >> 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid