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

Upload in a RadGrid and LinqToSQL

7 Answers 149 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Matthew D
Top achievements
Rank 1
Matthew D asked on 11 Aug 2009, 10:27 PM
Hello-
I have been trying to use the following example to help me build some new functions into my app but have been running into some issues.
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/raduploadinajaxifiedgrid/defaultcs.aspx?product=upload

Are there any new demos/examples showing how to integrate the Upload control with a RadGrid that is bound to either a LinqDatasource or an EFDatasource?

My app continues to blow "Object of type 'System.Byte[]' cannot be converted to type 'System.Data.Linq.Binary'" but all fields/mappings look correct.

Thanks
MJD



7 Answers, 1 is accepted

Sort by
0
Hector
Top achievements
Rank 1
answered on 30 Oct 2009, 03:56 PM
Hello,
I am having the exact same issue.  I used the code from the samples.  I have a GridView displaying a table in SQL Server with an Image type column.  When I try to insert or update a column and select the image to insert, I get the error:
Object of type 'System.Byte[]' cannot be converted to type 'System.Data.Linq.Binary'.
I am using a LinqToSql data source.  Here is my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageUpload.aspx.cs" Inherits="ImageUpload" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
      
    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">  
    </telerik:RadScriptManager> 
      
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
    </telerik:RadScriptBlock> 
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
 
        <script type="text/javascript">  
            // <![CDATA[
            //On insert and update buttons click temporarily disables ajax to perform upload actions
            function conditionalPostback(sender, eventArgs) {
                var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig");
                if (eventArgs.get_eventTarget().match(theRegexp)) {
                    var upload = $find(window['UploadId']);
                    //AJAX is disabled only if file is selected for upload
                    if (upload.getFileInputs()[0].value != "") {
                        eventArgs.set_enableAjax(false);
                    }
                }
            }
            function validateRadUpload(source, e) {
                e.IsValid = false;
                var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
                var inputs = upload.getFileInputs();
                for (var i = 0; i < inputs.length; i++) {
                    //check for empty string or invalid extension
                    if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value)) {
                        e.IsValid = true;
                        break;
                    }
                }
            }
            // ]]> 
        </script> 
 
    </telerik:RadCodeBlock> 
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="300px">  
        <telerik:RadProgressManager ID="RadProgressManager1" runat="server" /> 
        <telerik:RadProgressArea ID="RadProgressArea1" runat="server">  
        </telerik:RadProgressArea> 
    </telerik:RadAjaxPanel> 
    <br /> 
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="LinqDataSource1" GridLines="None" 
    AllowAutomaticInserts="true" 
            AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" ShowStatusBar="True" 
            OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated">  
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="ShopID" DataSourceID="LinqDataSource1">  
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridEditCommandColumn ButtonType="ImageButton">  
                    <HeaderStyle Width="3%" /> 
                </telerik:GridEditCommandColumn> 
                <telerik:GridBoundColumn DataField="ShopID" DataType="System.Int32" HeaderText="ShopID" 
                    ReadOnly="True" SortExpression="ShopID" UniqueName="ShopID">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" 
                    UniqueName="Name">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone" SortExpression="Phone" 
                    UniqueName="Phone">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Address" HeaderText="Address" SortExpression="Address" 
                    UniqueName="Address">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" 
                    UniqueName="City">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="State" HeaderText="State" SortExpression="State" 
                    UniqueName="State">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="ZIP" DataType="System.Decimal" HeaderText="ZIP" 
                    SortExpression="ZIP" UniqueName="ZIP">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" SortExpression="Description" 
                    UniqueName="Description">  
                </telerik:GridBoundColumn> 
                <telerik:GridBinaryImageColumn DataField="Image" HeaderText="Image" UniqueName="Image" ImageAlign="NotSet" 
                    ImageHeight="80px" ImageWidth="80px" ResizeMode="Fit" DataAlternateTextField="Description" 
                    DataAlternateTextFormatString="Image of {0}">  
                    <HeaderStyle Width="10%" />   
                </telerik:GridBinaryImageColumn> 
                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton">  
                    <HeaderStyle Width="2%" /> 
                </telerik:GridButtonColumn> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" 
        EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Shops">  
    </asp:LinqDataSource> 
    <div> 
    </div> 
    </form> 
</body> 
</html> 
 

and the code behind:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
public partial class ImageUpload : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Image") as GridBinaryImageColumnEditor;  
            RadAjaxPanel1.ResponseScripts.Add(string.Format("window['UploadId'] = '{0}';", editor.RadUploadControl.ClientID));  
        }  
    }  
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Image") as GridBinaryImageColumnEditor;  
            TableCell cell = (TableCell)editor.RadUploadControl.Parent;  
            CustomValidator validator = new CustomValidator();  
            validator.ErrorMessage = "Please select file to be uploaded";  
            validator.ClientValidationFunction = "validateRadUpload";  
            validator.Display = ValidatorDisplay.Dynamic;  
            cell.Controls.Add(validator);  
        }  
    }  
}  
 

I took this straight out of the samples.  Any help is greatly appreciated.
0
Michael
Top achievements
Rank 1
answered on 30 Oct 2009, 08:14 PM
I was having the exact same problem. I went into me dbml file and switched my Image field from a Linq.Binary to a System.Byte[] and all is well now.
0
Hector
Top achievements
Rank 1
answered on 30 Oct 2009, 11:26 PM
Thank you, Michael, it worked just like you said.
Much obliged!

-Hector
0
ephillipe
Top achievements
Rank 2
answered on 31 Oct 2009, 10:45 AM
I have same problem, but:
I use LinqToEntities and do not can change the type. 
0
Hector
Top achievements
Rank 1
answered on 31 Oct 2009, 12:22 PM
 EPhillipe, I tried that out on too and it works fine  I already have a lot of coding usinig the LinqToSQL datacontext that it would be more work to re-structure my code to use the L2E model.  But I am glad you pointed me to this because it is more powerful and flexible.  I will be using D2E more   Thanks.
0
Genady Sergeev
Telerik team
answered on 02 Nov 2009, 09:43 AM
Hi guys,

Byte[] does not map directly to System.Linq.Binary. In order to get a Binary object from a Byte[] one you need to call the Binary constructor. Example:


Byte[] byteArray = new Byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read(byteArray, 0, uploadedFile.ContentLength);
System.Linq.Binary file = new System.Linq.Binary(byteArray);

You can also take a look at this code library which demonstrates how to save files to a database using LINQ2SQL.


Kind regards,
Genady Sergeev
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
ephillipe
Top achievements
Rank 2
answered on 02 Nov 2009, 10:42 AM
Hi All,
I use Entity Framework with RadGrid. I use NeedDataSource event for fill Grid.
The problem is: Entity Framework maps Binary fields in database with Binary property in a model. I can't change this. And because this, I don't can use GridBinaryImageColumn.

Any suggestions?
Thanks.
Tags
Upload (Obsolete)
Asked by
Matthew D
Top achievements
Rank 1
Answers by
Hector
Top achievements
Rank 1
Michael
Top achievements
Rank 1
ephillipe
Top achievements
Rank 2
Genady Sergeev
Telerik team
Share this question
or