<
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
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
Marin
the Telerik team
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.
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.
Marin
the Telerik team
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.
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.
Marin
the Telerik team
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
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
;
}
}
}
Marin
the Telerik team