Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i am passing images from my local disk to crystal report using vb.net code, i have completed for passing one images from my local drive to crystal report, but the issue is i want to pass many images from same drive, i am using below coding for passing images..

VB
Dim dt As New DataTable
      ' object of data row
      Dim drow As DataRow
      ' add the column in table to store the image of Byte array type
      dt.Columns.Add("Image", System.Type.GetType("System.Byte[]"))
      drow = dt.NewRow
      ' define the filestream object to read the image
      Dim fs As FileStream
      ' define te binary reader to read the bytes of image
      Dim br As BinaryReader
      ' check the existance of image
      If File.Exists(AppDomain.CurrentDomain.BaseDirectory & "10157.Jpg") Then
          ' open image in file stream
          fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory & "10157.Jpg", FileMode.Open)
      Else ' if phot does not exist show the nophoto.jpg file
          fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory & "FLYWELL.jpg", FileMode.Open)
      End If
      ' initialise the binary reader from file streamobject
      br = New BinaryReader(fs)
      ' define the byte array of filelength
      Dim imgbyte(fs.Length) As Byte
      ' read the bytes from the binary reader
      imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)))
      drow(0) = imgbyte       ' add the image in bytearray
      dt.Rows.Add(drow)       ' add row into the datatable
      br.Close()              ' close the binary reader
      fs.Close()              ' close the file stream
      Dim rptobj As New CrystalReport1    ' object of crystal report
      rptobj.SetDataSource(dt)
      ' set the datasource of crystalreport object
      CrystalReportViewer1.ReportSource = rptobj  'set the report source
Posted
Updated 27-Mar-11 21:51pm
v3

1 solution

You've already got everything you need, you just need to reorganize it.
Put this code into a method that passes in the the file names, and the data table as parameters. Then call it for each file you want to add to the report. Move these statements to be called only once after you've called your method:
Dim rptobj As New CrystalReport1
rptobj.SetDataSource(dt)
CrystalReportViewer1.ReportSource = rptobj



--------------------------------------
Okay, I'll give you one more push in the right direction. Create a method like this:

VB
Private Sub AddImage(ByRef dt As DataTable, ByVal strFileName As String)
'Put your code in here, minus the three statements I mention above
'And change "10157.Jpg" to strFileName
End Sub


Now where you used to have your code, put something like this:
VB
Dim dt as New DataTable
AddImage(dt, "10157.Jpg")
AddImage(dt, "secondImage.jpg")
AddImage(dt, "thirdImage.jpg")

'Now those last three lines
Dim rptobj As New CrystalReport1
rptobj.SetDataSource(dt)
CrystalReportViewer1.ReportSource = rptobj


You'll have to work out any little bugs I've missed, but this should get you started.
 
Share this answer
 
v2
Comments
sameertm 28-Mar-11 9:42am    
can u give me some sample method for it, i don exactly how to do it so please guide me
Kschuler 28-Mar-11 9:50am    
I updated my solution with a bit more help.
sameertm 28-Mar-11 9:51am    
where i can find the solution ?
sameertm 28-Mar-11 9:58am    
its showing some error
Kschuler 28-Mar-11 10:15am    
You could post another question with the code you are using and the error message and probably get some help.

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