StrataFrame Forum

Deleting an Image from the DevEx PictureEdit Control

http://forum.strataframe.net/Topic13645.aspx

By Bill Cunnien - 1/22/2008

The image from the DB displays fine.  I can add/edit the image on the form.  It persists in the DB without errors.

If I choose CUT or DELETE from the context menu, I get a DBNull error popping up.  Of course, this is not expected (is it ever?) since I spent a good chunk of time yesterday wrestling with the null issue and the image.  I thought I had that thing licked.

Ermm

What can I do about the delete/cut process on the pictureedit control?

Thanks,
Bill

By Trent L. Taylor - 1/22/2008

What can I do about the delete/cut process on the pictureedit control?

Well, I am not sure off of the top of my head and will have to setup a test with this control.  Bottom line is that the PictureEdit control is trying to set the property to Null...you really want it to be any empty byte array:

New Byte() {}

Also, you may not want to directly bind to the control.  In this example you may want to handle the Navigated event (or something long those lines) and manually set the Picture (or whatever it is) property of the PictureEdit to avoid this type of error.  Just some ideas.

By Bill Cunnien - 1/23/2008

Here is the error:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

And, here's the code (offending line specified):

private class Field_sketch_Descriptor : MicroFour.StrataFrame.Business.FieldPropertyDescriptor
{
   
public Field_sketch_Descriptor() : base("sketch") { }
   
private System.Type _PropertyType = typeof(System.Byte[]);
   
public override object GetValue(Object component)
    {
       
return ((PartsBO)component).sketch;
    }
   
public override void SetValue(Object component, object Value)
    {
        ((
PartsBO)component).sketch = (System.Byte[])Value;  <---offending line
    }
   
public override System.Type PropertyType
    {
       
get
       
{
           
return this._PropertyType;
        }
    }
   
public override System.Type ComponentType
    {
       
get
       
{
           
return _ComponentType;
        }
    }
}

I must have done something different to my previous data table...like set all null values to an empty byte array, or something.  It worked before my recent re-import of the data.  I'll have to review my records to see what might have happened.

Would you suggest a default value for the 'image' field in a SQL Server data table?  Or, would a null value be acceptable?  If a null is acceptable, how should I handle it in the BO?  Then, how would I handle it in the PictureEdit control?

Thanks!
Bill

By Bill Cunnien - 1/23/2008

Without changing my database in any way I added this code:

private void SketchPictureEdit_EditValueChanged(object sender, EventArgs e)
{

   
if (SketchPictureEdit.EditValue == new byte[] {})
    {
       
this.partsBO1.sketch = new byte[] {};
        SketchPictureEdit.EditValue =
string.Empty;
   
}
   
else
   
{
       
this.partsBO1.sketch = (byte[])SketchPictureEdit.EditValue;
    }
}

This works.  I am still running some tests, but at least I have a way around the issue.

By Bill Cunnien - 1/23/2008

The first thing that the EditValueChanged event needs to check is the editing state of the BO.  If adding or editing, then the code can run; otherwise, it should skip it.  The editing state will be changed by the following line of code:

this.partsBO1.sketch = (byte[])SketchPictureEdit.EditValue;

That is why my navigation disappeared.  And, also explains the confirmation popup when closing the window.

Whew!  Glad that's over.
Bill

(note: cross-posted on purpose--http://forum.strataframe.net/Topic13453-7-1.aspx)

By StrataFrame Team - 1/24/2008

Since this error is happening within the Set, not the Get, it looks like the PictureEdit control is actually passing back a DBNull.Value out of its bound property.  Is there a NullValue property somewhere on the control where you can set what the control considers to be "null" to pass back to the data source?  Some of them allow you to pass back either a DBNull.Value or a "null" (C# keyword) or an empty value.
By Bill Cunnien - 1/24/2008

Nope...no NullValue property that I can discover.  There is a NullText property, but it isn't providing the binding value that I need.

Thanks for the help!
Bill

By Bill Cunnien - 1/24/2008

Oh...btw...I removed the following code (in red):

private void SketchPictureEdit_EditValueChanged(object sender, EventArgs e)
{

   
if (SketchPictureEdit.EditValue == new byte[] {})
    {
       
this.partsBO1.sketch = new byte[] {};
        SketchPictureEdit.EditValue =
string.Empty;
   
}
   
else
   
{
       
this.partsBO1.sketch = (byte
[])SketchPictureEdit.EditValue;
    }
}

Since I have the control bound to the data already, this was redundant.  And, it caused other navigation problems at various points in the user experience.

By Trent L. Taylor - 1/27/2008

Ah...yeah, that could cause an issue Smile  Glad you found it.