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

RadGrid with GridBinaryImageColumn Fails Update

10 Answers 176 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tony
Top achievements
Rank 1
tony asked on 05 May 2012, 09:41 PM
i have a radgrid one of whose columns is:
<telerik:GridBinaryImageColumn DataField="Media" DataType="System.Byte"
FilterControlAltText="Filter column3 column" ImageHeight="150px"
ImageWidth="150px" ResizeMode="Fit" UniqueName="Media"
HeaderText="Media" >
</telerik:GridBinaryImageColumn>

it is bound to an EntityDataSource.

using out of the box grid functionality, i click edit, select a file, then click update.
then i get the following error message: Error while setting property 'Media': 'The property 'Media' cannot be set to a null value.'.

why? having selected a file i would expect the control to send the data to the database. insert workds ok.





10 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 09 May 2012, 08:21 AM
Hi,

 It looks like the file is not uploaded correctly in this case. Do you use ajax in the grid? You can find more information on how to configure the RadBinaryImage in these live demos:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/raduploadinajaxifiedgrid/defaultcs.aspx?product=grid 
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandradasyncupload/defaultcs.aspx?product=grid 

All the best,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 09 May 2012, 04:32 PM
i am not using ajax - or at least i have done nothing to activate it. the radgrid does not have properties governing its activation. i am using the default autogenerated controls for gridbinaryimagecolumn which i do not believe has ajax enabled. even if they did, they are not accessible at design time.

the demos do not seem relevant to my situation for the primary reason that i am not using a radajaxpanel and consequently do not have method set_ajaxenabled(). besides radupload is not supposed to be an ajax enabled control as i understand it.

removing gridbinaryimagecolumn does allow updates to succeed.

ps.
i tried replicating the first example given and still get a failure. the only material difference between my code and yours is the data source. i use entitydatasource with entity framework while yours uses sqldatasource with sql. do you have an entitydatasource with ef example which works? your grid example is close to mine so i would interested to see if eds/ef make a difference. also,my image column is required.
0
Marin
Telerik team
answered on 11 May 2012, 05:09 PM
Hello,

 It is probably not an ajax issue since you have to ajaxified the control explicitly. I am attaching a sample runnable project which shows GridBinaryImageColumn and EntityDataSource. I checked the update functionality and it was working correctly there.
Have a look at it and let me know if there is any difference with your project.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 12 May 2012, 04:12 PM
just finished an interesting experiment with nullability.....using the website example you sent....employee table photo column is set to nullable....when set not null in the edm then the update fails....if nullable then succeeds....somehow the interface between the upload control and ef entity is not right.

in my model i had to change both the database and edm model for image column to not null.....doing so caused updates to work. both were originally not null.

i should be able to declare a table column (eg. image) not null without incurring an update error. it did not matter whether or not i chose a new image file. i woud consider this a defect. i am using ef 5 beta and sql server express 2012 with varbinary(max) data type rather than image type.
0
Marin
Telerik team
answered on 15 May 2012, 12:17 PM
Hello Tony,

 Indeed the problem is caused by the non-nullable field in the database. When the EntityDataSource performs an update operation it requires both the new and old values for an entity to be passed. In the case of GridBinaryImage column however there is no old value. The RadUpload control that is shown initially does not have any file selected - so it passes null as an old value of the field. The new value of course is properly initialized, but EF validates all the values and since the field in the database is non-nullable - an exception is thrown. The options here are either to have some default value for this filed in the entity or the upload control in the edit form or set the field in the database to be non-nullable.

I hope this helps.

Greetings,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 15 May 2012, 03:55 PM
i think you meant to say that the database column should be nullable as a non-nullable column will fail to update whether or not a file has been selected. i could never figure out how to set the default value programmatically or to supply the old value. these values are available in ef but i don't think supplying a file name is sufficient and certainly not desirable. one would need the image bytes passed to the control which interfaces with ef.
0
Marin
Telerik team
answered on 16 May 2012, 09:52 AM
Hello Tony,

 Yes, you are right. The field in the database should be nullable. As for the default value indeed one option is to set some default file to the RadUpload control when the edit form is opened, but this might not be very appropriate. Another option is to check when a null value is about to be set to the property of the entity in EF and then replace it with some default value (e.g. 0x0 which will indicate zero bytes)

Greetings,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 16 May 2012, 03:55 PM
why would i set the file to a default when the edit form is opened? i selected a value after it opened so its value is not null. if you tell me that it needs a non-null original value, then tell me where to set that. i can pull it from the ef state manager.
0
Marin
Telerik team
answered on 21 May 2012, 02:14 PM
Hi,

 The old values that are passed to the Update method of the EntityDataSourceView are extracted when the edit form is opened. However there is no easy way to replace this value when a null is coming from the RadUpload control.
You can either override the setter of the respective property in the generated entity. For example property Photo of entity Employee:

[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable = false)]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
    public byte[] Photo
    {
        get
        {
            return global::System.Data.Objects.DataClasses.StructuralObject.GetValidValue(this._Photo);
        }
        set
        {
            if (value==null)
            {
                value = new byte[0];
            }
            this.OnPhotoChanging(value);
            this.ReportPropertyChanging("Photo");
            this._Photo = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
            this.ReportPropertyChanged("Photo");
            this.OnPhotoChanged();
        }
    }

Or you can override the FillValues method of the GridBinaryImageColumn (in this case you would need a new column type that inherits the GridBinaryImageColumn as shown here):

public override void FillValues(IDictionary newValues, GridEditableItem editableItem)
        {
            if (editableItem.IsInEditMode)
            {
                GridBinaryImageColumnEditor editor = (GridBinaryImageColumnEditor)editableItem.EditManager.GetColumnEditor(this);
                if (editor.RadUploadControl.UploadedFiles.Count > 0)
                {
                    newValues[DataField] = editor.UploadedFileContent;
                }
                else
                {
                    //instead of null set new byte[0]
                    newValues[DataField] = null;
                }
            }
        }

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 24 May 2012, 11:19 PM
very good - thank you for that insight. for the time being i have changed my data schema but that may not always be possible or desirable in which case your method may be valuable. thank you.
Tags
Grid
Asked by
tony
Top achievements
Rank 1
Answers by
Marin
Telerik team
tony
Top achievements
Rank 1
Share this question
or