Loading image dynamically in crystal report
-
I have a field of Image path on database and I need to display the image of student in crystal report. I am using VS2005 with crystal report version 9. I have added the unbound column named "Image" of type system.byte in Data Set and at the load event, I have write the following code. Dim da As New DataSet1TableAdapters.AdmissionFormTableAdapter Dim dt As New DataSet1.AdmissionFormDataTable da.FillByStudentID(dt, _studentID) Dim dtCopy As New DataTable ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Code Start to Set Image ''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim blankFile As String = "C:\Program Files\blank.JPG" If dt.Rows(0).Item("ImagePath").ToString <> "" Then Dim s As String = dt.Rows(0).Item("ImagePath").ToString If FileIO.FileSystem.FileExists(s) Then Dim fs As FileStream = New FileStream(s, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() Else Dim fs As FileStream = New FileStream(blankFile, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() End If Else Dim fs As FileStream = New FileStream(blankFile, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Code End to Set Image ''''''''''''''''''''''''''''''''''''''''''''''''''''''' dtCopy = dt.Copy Me.rptAdmissionForm1.SetDataSource(dtCopy) Me.rptAdmissionForm1.SetParameterValue("ID", _studentID) When I run the code, error comes that "unable to cast type of system.byte to system.IConvertible", although the datatype of column "Image" is system.byte, and when I debug the code, it shows the datatype of dt.Rows(0).Item("Image") = System.DBNull, Please help me out..!
-
I have a field of Image path on database and I need to display the image of student in crystal report. I am using VS2005 with crystal report version 9. I have added the unbound column named "Image" of type system.byte in Data Set and at the load event, I have write the following code. Dim da As New DataSet1TableAdapters.AdmissionFormTableAdapter Dim dt As New DataSet1.AdmissionFormDataTable da.FillByStudentID(dt, _studentID) Dim dtCopy As New DataTable ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Code Start to Set Image ''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim blankFile As String = "C:\Program Files\blank.JPG" If dt.Rows(0).Item("ImagePath").ToString <> "" Then Dim s As String = dt.Rows(0).Item("ImagePath").ToString If FileIO.FileSystem.FileExists(s) Then Dim fs As FileStream = New FileStream(s, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() Else Dim fs As FileStream = New FileStream(blankFile, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() End If Else Dim fs As FileStream = New FileStream(blankFile, FileMode.Open, FileAccess.Read) Dim image(fs.Length) As Byte fs.Read(image, 0, Convert.ToInt32(fs.Length)) dt.Rows(0).Item("Image") = image fs.Close() End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Code End to Set Image ''''''''''''''''''''''''''''''''''''''''''''''''''''''' dtCopy = dt.Copy Me.rptAdmissionForm1.SetDataSource(dtCopy) Me.rptAdmissionForm1.SetParameterValue("ID", _studentID) When I run the code, error comes that "unable to cast type of system.byte to system.IConvertible", although the datatype of column "Image" is system.byte, and when I debug the code, it shows the datatype of dt.Rows(0).Item("Image") = System.DBNull, Please help me out..!
Your code is very confusing as you have variables named the same but at different levels. It would be easier to find the problem if you would have just one main variable for the filestream, one for the image byte array. Use if statements to determine which file you want to load, then when you've done all the checks you can have just one spot that actually uses the filestream reader code. You might also want to try using this instead of your filestreamreader code to convert the file into a byte array: If FileIO.FileSystem.FileExists(s) Then System.IO.File.ReadAllBytes(s) End If Also, is there a reason you make a copy of the datatable to send in, instead of just sending dt? Hope this helps.