Insert Binary Image - RadGrid

3 posts, 0 answers
  1. Samantha
    Samantha avatar
    21 posts
    Member since:
    Apr 2009

    Posted 10 Jan 2011 Link to this post

    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"
                           <asp:SessionParameter Name="SSN" SessionField="SSN" Type="String" />
                           <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" />

    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. Samantha
    Samantha avatar
    21 posts
    Member since:
    Apr 2009

    Posted 11 Jan 2011 Link to this post

    Pretty Please?  :)
  3. DevCraft R3 2016 release webinar banner
  4. Iana Tsolova
    Iana Tsolova avatar
    3388 posts

    Posted 14 Jan 2011 Link to this post

    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,
    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.
Back to Top