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

Insert Binary Image - RadGrid

2 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Samantha
Top achievements
Rank 1
Samantha asked on 10 Jan 2011, 08:44 PM
I am trying to insert a file (PDF/JPG/XLS) into an image field in my SQL database.  Thus far whatever I do, the file is inserted as a NULL value. The GridViewDataHyperLinkColumn is part of a nested detail table.  All other fields update just fine.  

<asp:SqlDataSource ID="sqlGetDocs" runat="server" OnInserting="SqlGetDocs_Updating"
                   ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
                   SelectCommand="spAspGetEnrollDocs" SelectCommandType="StoredProcedure"
                   InsertCommand="spASPinsertClientDocDatabase"
                   InsertCommandType="StoredProcedure">
                   <SelectParameters>
                       <asp:SessionParameter Name="SSN" SessionField="SSN" Type="String" />
                   </SelectParameters>
                   <InsertParameters>
                       <asp:ControlParameter ControlID="DropDownList1" Name="Client"
                           PropertyName="SelectedValue" Type="String" />
                       <asp:Parameter Name="SSN" Type="String" />
                       <asp:Parameter Name="DocDesc" Type="String" />
                       <asp:Parameter Name="FileType" Type="String" />
                       <asp:Parameter Name="ImageFile" dbType="Binary" />
                       <asp:Parameter Name="Updated" Type="DateTime" />
                       <asp:Parameter Name="Comments" Type="String" />
                       <asp:ControlParameter ControlID="lblUserName" Name="UserID" PropertyName="Text"
                           Type="String" />
                   </InsertParameters>
               </asp:SqlDataSource>

If I add the following code - I receive an error where I declare "imageBytes":  

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Protected Function UpdateImageField(ByVal gridEditableItem As GridEditableItem) As Byte()
    Dim bytes As Byte() = (DirectCast(gridEditableItem.EditManager.GetColumnEditor("ImageFile"), GridBinaryImageColumnEditor)).UploadedFileContent
    If bytes Is Nothing OrElse bytes.Length = 0 Then
        bytes = New Byte() {}
    End If
    Return bytes
End Function
 
Protected Sub SqlGetDocs_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
    Dim imageBytes As Byte() = UpdateImageField(TryCast(RadGrid.EditItems(0), GridEditableItem))
    e.Command.Parameters("@ImageFile").Value = imageBytes
End Sub

Could someone tell me what I'm doing wrong?

2 Answers, 1 is accepted

Sort by
0
Samantha
Top achievements
Rank 1
answered on 11 Jan 2011, 07:35 PM
Pretty Please?  :)
0
Iana Tsolova
Telerik team
answered on 14 Jan 2011, 08:27 AM
Hi Samantha,

In order to achieve your goal, I suggest that you use a GridTemplateColumn with RadUpload in the EditItemTemplate. Then handle the RadGrid UpdateCommand and get the byte array value of the RadUpload to set it as default value of the ImageFile Parameter. In the grid UpdateCommand event the grid editable item is accessible and you can use similar code as you tried in the SqlDataSource Updating command.

Kind regards,
Iana
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Samantha
Top achievements
Rank 1
Answers by
Samantha
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or