This is a migrated thread and some comments may be shown as answers.

Picturebox with databound url not working

6 Answers 373 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
john81
Top achievements
Rank 1
john81 asked on 18 Jan 2010, 09:21 PM
I'm trying to dynamically bind a picturebox to images on my web site using a url.  The image file which may or may not be there is dependent on a report field.  I'm added an ItemDataBound event to the class with the following code:

private void pictureBox1_ItemDataBound(object sender, EventArgs e)
{
     this.pictureBox1.Value = "http://mywebsite.com/images/" + "=Fields.pic_name" + ".png";
}

But it's not working and I'm getting  "The path is not of a legal form" error.  Even when I try directly entering a url of an image I know is on the web site, I'm still getting the same error.



6 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 21 Jan 2010, 02:11 PM
Hello John,

You are trying to set the value of the definition PictureBox item, while when you are in an event you should always work with the processing report object (more info on definition vs. processing is available in Understanding Events help article).
In your exact case you combine a field value with URI path, which would be better handled directly as expression in the PictureBox item i.e.:

= "http://mywebsite.com/images/" + Fields.pic_name + ".png";

Regards,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
JohnH
Top achievements
Rank 1
answered on 22 Jan 2010, 08:39 PM
Understand I can assign a combined uri with a field in the picturebox value.  I've got that working.

The problem with that is all my images referenced in my database don't exist so for those that don't I'm getting a big error box on the report.  That's why I wanted to use ItemDataBound to check and see if the image file exists before data binding the picture.  But I've had no luck getting that working.  I've got code in ItemDataBinding working that is below but the problem with this code is it seems to pull the image for the previous item.  I'm guessing that's because ItemDataBinding occurs before the data is bound so I won't have the correct picture id.  How can I get this working in ItemDataBinding or do something similar in ItemDataBound???

        private void pictureBox1_ItemDataBinding(object sender, EventArgs e)
        {
            Telerik.Reporting.Processing.ReportItemBase itemBase = (Telerik.Reporting.Processing.ReportItemBase)sender;
            string strID = (string)((System.Data.DataRowView)(itemBase.DataItem))["lin_id"];
            string strIconFile = "C:\\Web\\Images\\Icons\\128x128\\" + strID + ".png";
            if (File.Exists(strIconFile))
            {
                this.pictureBox1.Value = strIconFile;
            }
            else
            {
                this.pictureBox1.Value = "C:\Web\\Images\\Icons\\NOIMAGE.png";
            }
        }

0
Accepted
Steve
Telerik team
answered on 25 Jan 2010, 02:20 PM
Hi John Hadzima,

As explained in this blog post, there are various ways you can show images in the picturebox report item. There are several possible approaches for your scenario, but creating an user function seems like the way to go:

public static Image LoadImage(string imageLocation)       
                 {
                string absoluteLocation = "C:\\Web\\Images\\Icons\\" + imageLocation;
                if (File.Exists(absoluteLocation))
                {
                    return Image.FromFile(absoluteLocation);
                }
                else
                {
                    return Image.FromFile(@"C:\\Web\\Images\\Icons\\NOIMAGE.png");
                }    
            }
and you set the value of the picturebox item to "=LoadImage(Fields.pic_name)".

We've already explained that when you're in the context of events you should use the processing object and not the definition. However the processing object accepts only an image and you cannot feed it with the string path to your files.

Sincerely yours,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
john81
Top achievements
Rank 1
answered on 25 Jan 2010, 02:41 PM
I created the user function and set the value of the picturebox.  Seems pretty straight forward but now I'm getting a different error.

An error has occured while executing the function LoadImage().  Check InnerException for further information.
0
Steve
Telerik team
answered on 25 Jan 2010, 02:53 PM
Hello John,

Set a breakpoint in your user function and debug it. Most likely the paths are not correct - I've used them as you have sent them in your previous reply, but I am not aware of whether they are correct or not on your end.

Sincerely yours,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
hashiny
Top achievements
Rank 1
answered on 19 Nov 2010, 08:22 AM
thank you for your answer, when we use the "path = "=//JITSERVER/digitalfilesfmsdemo/" + Fields.imgpath;"
   the error is shown as "  The name 'Fields' does not exist in the current context ",
why it comes?and what could be the solution

Tags
General Discussions
Asked by
john81
Top achievements
Rank 1
Answers by
Steve
Telerik team
JohnH
Top achievements
Rank 1
john81
Top achievements
Rank 1
hashiny
Top achievements
Rank 1
Share this question
or