Unplanned
Last Updated: 29 May 2020 15:15 by ADMIN
Panayotis
Created on: 11 Feb 2019 10:24
Category: Grid
Type: Feature Request
1
GridAttachmentColumn passes malformed file path to the DataSource
GridAttachmentColumn malforms the String filepath, from "C\SomePath\SomeFile.PDF" to something like "C:" & vbBack &"SomePathSomeFile.PDF"

Steps to reproduce the error:

DataSource Bound to grid:

Private Function SomeTable() As DataTable
    Dim dt As New DataTable()
 
    dt.Columns.Add(New DataColumn("FieldName", Type.GetType("System.String")))
 
    row("FieldName") = "C:\SomeDirectory\RadGridExport.pdf"
    dt.Rows.Add(row)
 
    Return dt
End Function

GridAttachmentColumn markup as well as the ObjectDataSource that returns a byte array when user clicks on download attachment:

<telerik:GridAttachmentColumn DataSourceID="ObjectDataSource1"
    HeaderText="Attachment Column"
    AttachmentKeyFields="FieldName"
    AttachmentDataField="FieldName"
    DataTextField="FieldName"
    UniqueName="FieldName"
    ButtonType="ImageButton"
    UploadControlType="RadAsyncUpload"
    FileName="RadGridExport.pdf"
    ImageUrl="pdf-icon.png"
    ItemStyle-Height="36px"
    ItemStyle-Width="36px" >
</telerik:GridAttachmentColumn>
                         
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="MySelectMethod" TypeName="MyApp">
    <SelectParameters>
        <asp:Parameter Name="FieldName" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

MyApp.MySelectMethod that is supposed access the file by "filePath" and read it to byte, but the filePath is malformed, hence throws an exception.

Public Class MyApp
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)>
    Public Function MySelectMethod(filePath As String) As DataTable
        Dim dt As New DataTable("Base")
        Dim col As New DataColumn("FieldName")
        col.DataType = System.Type.GetType("System.Byte[]")
        dt.Columns.Add(col)
        Dim row As DataRow = dt.NewRow
        row(0) = My.Computer.FileSystem.ReadAllBytes(filePath)
 
        dt.Rows.Add(row)
        Return dt
    End Function
End Class


1 comment
ADMIN
Peter Milchev
Posted on: 29 May 2020 15:15

A temporary workaround:

Using the ItemCommand event handler of the Grid, you can get the correct Path (value) and save it in a flag variable (filePath) like the example below:

Dim filePath As String
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "DownloadAttachment" Then
        filePath = DirectCast(e.Item, GridDataItem).GetDataKeyValue("PDF")
    End If
End Sub

Wire up the OnSelecting server event to the ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetPDFByFileName" TypeName="MyApp"
    OnSelecting="ObjectDataSource1_Selecting">
    <SelectParameters>
        <asp:Parameter Name="PDF" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

In the Selecting event handler, access the value of the flag (filePath) and change the parameters for the PDF object, to send the correct format into the code behind:

Protected Sub ObjectDataSource1_Selecting(sender As Object, e As ObjectDataSourceSelectingEventArgs)
    If Not String.IsNullOrEmpty(filePath) Then
        Dim params As OrderedDictionary = e.InputParameters
        params("PDF") = filePath
    End If
End Sub

 

Regards,
Peter Milchev
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.