Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with viewing images in an MSReport using a Dataset using VB.NET

Hi,

I'm using the code below to view images whose file names are in my xml file and the images are listed in my application's folder, but I'm unable to view the images. How do I solve this issue?

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim ds = New DataSet()
        ds.ReadXml(Application.StartupPath + "\linkSearch.xml")
        ds.WriteXmlSchema(Application.StartupPath + "\linkSearch.xsd")

        Dim curfile As String = (Application.StartupPath + "\linkSearch.xml")
        fslinkBEL = New System.IO.FileStream(Application.StartupPath + "\LinkSearch.xml", IO.FileMode.Open)
        dtsetlinkBEL.Clear()
        dtsetlinkBEL.ReadXml(fslinkBEL)
        fslinkBEL.Close()

        Dim FilteredDT As DataTable
        Dim DV As New DataView(dtsetlinkBEL.Tables(0), Nothing, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        If FilteredDT.Rows.Count > 0 Then
            Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
            reportDataSource.Name = "DataSet1"
            reportDataSource.Value = FilteredDT
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
            ReportViewer1.LocalReport.Refresh()
            ReportViewer1.RefreshReport()
        End If
    End Sub

Thanks,

Victor
Avatar of HainKurt
HainKurt
Flag of Canada image

I dont see any code related to image or path...
Avatar of Victor  Charles

ASKER

The image is connected to the  image field in the rdlc file.
but it is not an image! it is just path to the image...

so I believe you need to set the path/src of image correctly

if path in db is "image1.jpg", and images are stored in "pictures" folder on your server,
maybe you should set the src of image as "http://domain_or_server/pictures/image1.jpg"
I can't  hard code tne image names because as I move to each revord I need to see whatever image is in the xml file.
This is a windows apication, need to set path to application's folder without hard coding name of the images.
Thanks.
ok then you need to write some code and say

image.path = app.path + ds["imgPath"]

or loop all records in FilteredDT and change path to app.path + ds["imgPath"] before binding to report...

or something like that... I dont think db has full path to file...
Image.path? How do you  declare image before using it in the code?
after this line

FilteredDT = DV.ToTable

you can change the values

for each row in FilteredDT.rows row.imgPath=app.path & row.imgPath

then bind it to report...
Thanks, I will try it when I get home. Shouldn't the code include the "Image" field which contains the images' names?
I tried your suggestion as shown in code below but getting error message:
An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'imgPath' on type 'DataRow' not found.

On line: row.imgPath=app.path & row.imgPath

Dim FilteredDT As DataTable
        Dim DV As New DataView(dtsetlinkBEL.Tables(0), Nothing, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        For each row in FilteredDT.rows
        row.imgPath=app.path & row.imgPath
        If FilteredDT.Rows.Count > 0 Then
            Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
            reportDataSource.Name = "DataSet1"
            reportDataSource.Value = FilteredDT
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
            ReportViewer1.LocalReport.Refresh()
            ReportViewer1.RefreshReport()
        End If
      Next
I gave you pseudo code, not exact code!

path of app can be retrieved like this

dim path  As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

Open in new window

or
dim path As String = Application.StartupPath()

Open in new window

or
dim path As String = Application.ExecutablePath()

Open in new window


then for each row change the value before binding

For each row in FilteredDT.rows 
        row("imgPath").value=path & row("imgPath").value

Open in new window


and replace imgPath with your column name...

by the way,

where are your images
how are they stored in db
what column stores those values?
The images are in my application folder in an image subfolder. The image names are in my xml file (Image data element) which was converted to .xsd to include in the project. I will try your code and get back to you.
Thanks.
so, this should work

dim path As String = Application.ExecutablePath()
For each row in FilteredDT.rows 
        row("Image").value=path & "image\" & row("Image").value

Open in new window

Hi,

Unfortunately it did not work. I received error message:

An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'value' on type 'String' not found.

On line:

row("Image").value = path & "Image\" & row("Image").value



code:
Dim ds = New DataSet()
        ds.ReadXml(Application.StartupPath + "\linkSearch.xml")
        ds.WriteXmlSchema(Application.StartupPath + "\linkSearch.xsd")

        Dim curfile As String = (Application.StartupPath + "\linkSearch.xml")
        fslinkBEL = New System.IO.FileStream(Application.StartupPath + "\LinkSearch.xml", IO.FileMode.Open)
        dtsetlinkBEL.Clear()
        dtsetlinkBEL.ReadXml(fslinkBEL)
        fslinkBEL.Close()

        Dim FilteredDT As DataTable
        Dim DV As New DataView(dtsetlinkBEL.Tables(0), Nothing, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        MsgBox(FilteredDT.Rows.Count)
        Dim path As String = Application.ExecutablePath()
        For Each row In FilteredDT.Rows
            row("Image").value = path & "Image\" & row("Image").value
           
                If FilteredDT.Rows.Count > 0 Then
                    Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
                    reportDataSource.Name = "DataSet1"
                    reportDataSource.Value = FilteredDT
                    ReportViewer1.LocalReport.DataSources.Clear()
                    ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
                    ReportViewer1.LocalReport.Refresh()
                    ReportViewer1.RefreshReport()
                End If
            Next
then dont use value!

row("Image").value = path & "Image\" & row("Image").value

>>>

row("Image") = path & "Image\" & row("Image")
Hi,

Below is the code and xml file containing the images for your information, hopefully you can duplicate the project from your end and test it with an image file. I am not able to upload the load the project.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.ReportViewer1.RefreshReport()
    End Sub

    Private Sub ReportViewer1_Load(sender As Object, e As EventArgs)

    End Sub

    Public fslinkBEL As System.IO.FileStream
    Public dtsetlinkBEL As New DataSet
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim ds = New DataSet()
        ds.ReadXml(Application.StartupPath + "\linkSearch.xml")
        ds.WriteXmlSchema(Application.StartupPath + "\linkSearch.xsd")

        Dim curfile As String = (Application.StartupPath + "\linkSearch.xml")
        fslinkBEL = New System.IO.FileStream(Application.StartupPath + "\LinkSearch.xml", IO.FileMode.Open)
        dtsetlinkBEL.Clear()
        dtsetlinkBEL.ReadXml(fslinkBEL)
        fslinkBEL.Close()

        Dim FilteredDT As DataTable
        Dim DV As New DataView(dtsetlinkBEL.Tables(0), Nothing, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        MsgBox(FilteredDT.Rows.Count)
        Dim path As String = Application.ExecutablePath()
        For Each row In FilteredDT.Rows
            row("Image").value = path & "Image\" & row("Image").value
           
                If FilteredDT.Rows.Count > 0 Then
                    Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
                    reportDataSource.Name = "DataSet1"
                    reportDataSource.Value = FilteredDT
                    ReportViewer1.LocalReport.DataSources.Clear()
                    ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
                    ReportViewer1.LocalReport.Refresh()
                    ReportViewer1.RefreshReport()
                End If
            Next


    End Sub
End Class


LinkSearch.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<MyDataTable>
    <LinkAID>1</LinkAID>
    <Image>ImageA.jpg</Image>
</MyDataTable>
<MyDataTable>
    <LinkAID>2</LinkAID>
    <Image>ImageB.jpg</Image>
</MyDataTable>
<MyDataTable>
    <LinkAID>3</LinkAID>
    <Image>ImageC.jpg</Image>
</MyDataTable>
<MyDataTable>
    <LinkAID>4</LinkAID>
    <Image>ImageD.jpg</Image>
</MyDataTable>
</Root>
forms.pptx
I tried your suggestion, no errors occurred but the images are not displayed.
then debug your app

and see what is assigned here

row("Image") = path & "Image\" & row("Image")

is that correct?
I am getting the wrong path

Debug\WindowsApplication2.EXEImage\ImageA.jpg    

Victor
When I try the code below to obtain the correct path, the images are still not displayed.

  Dim path As String = Application.StartupPath()
        For Each row In FilteredDT.Rows
            row("Image") = path & "\Image\" & row("Image")
try using Application.StartupPath

Dim path As String = Application.StartupPath
        For Each row In FilteredDT.Rows
            row("Image").value = path & "Image\" & row("Image").value

Open in new window

It still doesn't work, removed .Value to avoid the error but still doesn't work, the path is correct bu for some reason the images does not display, could it be a property needs to be set to view images?
if path is correct, then play with report parameters for image field...
Trying different things in image properties but so far no luck.
try setting this

myReport.EnableExternalImages=true

check this video
https://www.youtube.com/watch?v=D4uU25NUy_4
Hi,

I followed the example for viewing external images, but the example is only for hard coding the external image in the code. How do I modify the code to include row (image) in the r(0)?. The code below in part B does not work, can't figure out how to include row(image) values in the following code:  r(0) = New ReportParameter("ImageVar", row("Image"), True). Code in Part A shows code in the video for hard coding an external file.

Part A:

 Dim r(0) As ReportParameter
        r(0) = New ReportParameter("ImageVar", "files\" + "imagepath\image.jpg", True)
        ReportViewer1.LocalReport.EnableExternalImages = True
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {r(0)})

Part B

  Dim r(0) As ReportParameter
        ReportViewer1.LocalReport.EnableExternalImages = True
        Dim path As String = Application.StartupPath
        For Each row In FilteredDT.Rows
            row("Image") = path & "\Image\" & row("Image")
            r(0) = New ReportParameter("ImageVar", row("Image"), True)
            If FilteredDT.Rows.Count > 0 Then

                Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
                reportDataSource.Name = "DataSet1"
                reportDataSource.Value = FilteredDT

                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
                ReportViewer1.LocalReport.Refresh()
                ReportViewer1.RefreshReport()
            End If
        Next

Thanks,

Victor
HELP!
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think the report parameter r(0) needs to be part of the code, similar to the example, only difference is solution of for specific image, I need to pass image value for each record.
Thanks,
Victor