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

How to handle a databound PictureBox when image is null?

8 Answers 1137 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
justins
Top achievements
Rank 1
justins asked on 11 Apr 2007, 07:20 PM
Hello again,
This forum has been very helpful.  I have a report with PictureBox control on it.  The report is bound to a data source and that data source includes an image from a SQL 2000 database.  I can successfully bind the PictureBox to the image by setting the PictureBox value to the data bound column of the image.

My problem is the image column this gets bound to is a nullable field.  When there is a null image returned in my data set, I receive an error stating the PictureBox can't be bound to a null field.  What is the best way to work arond this?  I'm thinking of dynamically adding the PictureBox to the report if an image exists but am not sure how to implement this.  thanks again!  Justin

8 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 12 Apr 2007, 04:43 PM
Hi Justin,

The wrong handling of the Null data fields for the PictureBox item is our fault and it will be fixed for the next build of the Reporting.

At the moment the only workaround is to insert dummy images in the place of the Null entries. This might be done either programmatically using the code snippet bellow or images can be inserted directly into the database.

The idea of the snippet is to handle the ItemDataBinding event of the Detail section and assign an empty bitmap on places where our Image data field is equal to DBNull.

 private void detail_ItemDataBinding(object sender, EventArgs e) 
 {             
      Telerik.Reporting.Processing.DetailSection section = 
                (Telerik.Reporting.Processing.DetailSection)sender; 
 
      DataRowView currentRow = (DataRowView) section.DataItem; 
 
      if (DBNull.Value == currentRow["LargePhoto"]) 
      { 
          using (Bitmap bmp = new Bitmap(2, 2)) 
          { 
              using (MemoryStream ms = new MemoryStream()) 
              { 
                  bmp.Save(ms, ImageFormat.Png); 
                  currentRow["LargePhoto"] = ms.ToArray(); 
              } 
          } 
       } 
}        



All the best,
Chavdar
the telerik team


Instantly find answers to your questions at the new telerik Support Center
0
Sascha
Top achievements
Rank 1
answered on 22 Nov 2011, 01:50 AM
I know this is a very old thread but was this issue ever fixed?

I appear to be hitting up against the same issue. My image is a binary byte[] (I can convert it to an image object if required but would prefer to keep it as a byte[]). If the data is null it still appears as an empty byte[] and throws an index out of bounds type error when rendering.

I am using the latest version of Telerik reporting. In case it is relevant, my picturebox is in the reportheader section - but it displays correctly when the image is not null. The error is thrown even if the picturebox visibility is set to false.
0
Steve
Telerik team
answered on 24 Nov 2011, 10:40 AM
Hello Sascha,

Yes the problem was fixed since, but resurfaced in the latest Q3 2011 version and would be fixed in an internal build later today. If you're going in production and do not want to use internal build, please downgrade to Q2 SP1 2011 where this problem does not exist.

Sorry for the temporary inconvenience.

All the best,
Steve
the Telerik team

Q3’11 of Telerik Reporting is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

0
Phil
Top achievements
Rank 1
answered on 01 Aug 2015, 06:09 PM
Is it possible the problem has re-re-surfaced in Q2 2015?  I have the same problem when my user function returns byte[]=null. I had to implement an empty image as Chavdar suggested back in 2007.
0
Stef
Telerik team
answered on 05 Aug 2015, 10:24 AM
Hello Phil,

Please test upgrading the project to the latest available Telerik Reporting Q2 2015 SP1 v9.1.15.731. Then set the image as PictureBox.Value directly or use a custom function.

If there is an issue with displaying the image, please post additionally screenshots and the logic for loading the image.

Regards,
Stef
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Phil
Top achievements
Rank 1
answered on 27 Aug 2015, 11:17 PM

Stef, sorry for the delayed response, working to meet a deadline, you know how those things go. If  I don't create an empty image, as in the previous post,  when the reader==DBNull.Value and simply return an empty Byte[] I get:

An error has occurred while processing PictureBox 'pictureBox1': Invalid image
data. ------------- InnerException ------------- Parameter is not valid.

My custom function basics are:

Byte[] sketch = new Byte[] { };
...
reader = sketchCMD.ExecuteReader();
if (reader.Read())
{
    if (reader["SKETCH"] != DBNull.Value)
    {
        sketch = (byte[])reader["SKETCH"];
    
 }
 reader.Close();
 sketchConn.Close();
 return sketch;

0
Nasko
Telerik team
answered on 01 Sep 2015, 01:23 PM
Hello Phil,

The issue was present in Q2 2015 but was fixed in Q2 2015 SP1 and the PictureBox should be working correctly with Null values in the latest release.
In case you have observed a different behavior in the latest version, please post a bug report in our system and attach a runnable sample project exhibiting the issue.

Regards,
Nasko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Phil
Top achievements
Rank 1
answered on 07 Sep 2015, 03:25 PM

Nasko provided the solution in a tech support request.  Simply return null; if the data is null.

 

private void detail_ItemDataBinding(object sender, EventArgs e) 
 {             
      Telerik.Reporting.Processing.DetailSection section = 
                (Telerik.Reporting.Processing.DetailSection)sender; 
   
      DataRowView currentRow = (DataRowView) section.DataItem; 
   
      if (DBNull.Value == currentRow["LargePhoto"]) 
      
          return null;
      
}   

Tags
General Discussions
Asked by
justins
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
Sascha
Top achievements
Rank 1
Steve
Telerik team
Phil
Top achievements
Rank 1
Stef
Telerik team
Nasko
Telerik team
Share this question
or