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

View file in sql table with link button

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
SMc asked on 12 May 2020, 07:19 PM

Hello, I am trying to create a way for the user to view a pdf, xlsx, jpg, gif, etc., from a link button within my Detail table in a radgrid. I am able to accomplish this for a pdf, but not the image files or Excel files.

One of the main issues is that the file types stored within the SQL table field can be any of those above types. My question is how can I write the vb.net code to display any type of file from the same RadGrid column; as the Details Table can have numerous records of different file types. 

Here's what I have --

01.<DetailTables>
02.                <telerik:GridTableView DataKeyNames="fileID" Name="Attachments" DataSourceID="sdsVenFiles" CommandItemDisplay="Top" Caption="Files Attached to this Vendor Maintenance Request:"
03.                    NoDetailRecordsText="No files are attached to this Vendor Adjustment request." Width="75%" EditFormSettings-FormCaptionStyle-Font-Bold="true">
04.                    <ParentTableRelation>
05.                        <telerik:GridRelationFields DetailKeyField="wfID" MasterKeyField="wfID" />
06.                    </ParentTableRelation>
07.                    <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="true" />
08.                    <Columns>
09.                        <telerik:GridBoundColumn SortExpression="fileID" HeaderText="File ID" HeaderButtonType="TextButton" DataField="fileID" Visible="false"></telerik:GridBoundColumn>
10.                        <telerik:GridBoundColumn SortExpression="wfID" HeaderText="Control #" HeaderButtonType="TextButton" DataField="wfID" Visible="false"></telerik:GridBoundColumn>
11.                        <telerik:GridTemplateColumn HeaderText="Open File">
12.                            <ItemTemplate>
13.                                <asp:LinkButton ID="lnkDownload" runat="server" Text='<%# Eval("fileName")%>' OnClick="viewFile" CommandArgument='<%# Eval("fileID")%>'></asp:LinkButton>
14.                            </ItemTemplate>
15.                        </telerik:GridTemplateColumn>
16.                         
17.                        <telerik:GridButtonColumn CommandName="Delete" ConfirmText="Delete this file?" HeaderText="Delete" Text="Delete" UniqueName="DeleteColumn1">
18.                        </telerik:GridButtonColumn>
19.                    </Columns>
20.                </telerik:GridTableView>
21.            </DetailTables>

 

And my vb.net --

01.Protected Sub viewFile(sender As Object, e As EventArgs)
02. 
03.        Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
04.        Dim bytes As Byte()
05.        Dim fileName As String, contentType As String
06.        Dim constr As String = ConfigurationManager.ConnectionStrings("DbName").ConnectionString
07.        Using con As New SqlConnection(constr)
08.            Using cmd As New SqlCommand()
09.                cmd.CommandText = "SELECT fileID, fileName, fileType, fileData FROM tblVendorFile WHERE fileId=@Id"
10.                cmd.Parameters.AddWithValue("@Id", id)
11.                cmd.Connection = con
12.                con.Open()
13.                Using sdr As SqlDataReader = cmd.ExecuteReader()
14.                    sdr.Read()
15.                    bytes = DirectCast(sdr("fileData"), Byte())
16.                    contentType = sdr("fileType").ToString()
17.                    fileName = sdr("fileName").ToString()
18.                End Using
19.                con.Close()
20.            End Using
21.        End Using
22. 
23.        Response.Clear()
24.        Response.Buffer = True
25.        Response.Charset = ""
26.        Response.Cache.SetCacheability(HttpCacheability.NoCache)
27.        Response.ContentType = "application/pdf"
28.        Response.BinaryWrite(bytes)
29.        Response.Flush()
30.        Response.End()
31. 
32.    End Sub

 

Thanks for any and all help!

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 15 May 2020, 05:20 PM

Hi Sean,

I will help you troubleshoot the issue in the Support Ticket you have submitted. Once we have the answer, I will share it in this forum thread as well.

Kind regards,
Doncho
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.
Tags
Grid
Asked by
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Doncho
Telerik team
Share this question
or