RadpdfViewer load data from DB (Datatype-varbinary(MAX))

0 Answers 461 Views
PdfViewer and PdfViewerNavigator
Arun
Top achievements
Rank 2
Iron
Arun asked on 14 Jun 2022, 07:20 AM | edited on 15 Jun 2022, 06:00 AM

Hi

Please Help

Language: VB.Net (VS2017)

Telerik Version: 2017.3.1017

Application Type: Windows Desktop

Looking for a solution to read the PDF file stored in SqlServer as varbinary(MAX). \

I have saved the pdf file as follows.

Private Sub Save_File() Dim fd As OpenFileDialog = New OpenFileDialog() fd.Filter = "pdf file|*.pdf"

If fd.ShowDialog = System.Windows.Forms.DialogResult.OK Then

Dim filebyte AsByte() = Nothing

Dim con As SqlConnection = New SqlConnection(ConnStr) Dim cmd As SqlCommand = Nothing filebyte = File.ReadAllBytes(fd.FileName) cmd = New SqlCommand("Insert into tbl_Doc (LYR_Doc) Values(@pdf)", con) cmd.Parameters.Add("@pdf", SqlDbType.Binary).Value = filebyte con.Open() cmd.ExecuteNonQuery() con.Close() Interaction.MsgBox("File saved into database", MsgBoxStyle.Information) EndIf

EndSub

Now I need to read this data and load in the pdfviewer.

I tried as follows. But no result.

 Dim eXactConnection As New SqlClient.SqlConnection(ConnStr)
        Dim eXactCommand As New SqlClient.SqlCommand("SELECT LYR_Doc from tbl_Doc where LYR_Name =  'Arun'", eXactConnection)
        eXactConnection.Open()
        If Not IsDBNull(eXactCommand.ExecuteScalar()) Then
            Dim Docdata As Byte() = DirectCast(eXactCommand.ExecuteScalar(), Byte())
            Using stream As New IO.MemoryStream(Docdata)
                RadPdfViewer1.LoadDocument(stream)
            End Using
        End If

 

Thanks in advance.

Arun Madhav


Nikolay Demirev
Telerik team
commented on 16 Jun 2022, 01:52 PM

Hi Arun,

I suggest you export the data you read from the SqlServer to a pdf file on your file system and try to open it in any Pdf viewer, so you can validate if the data read from the DB is correct.

Another thing you could try is to move the stream position to the beginning of the stream before passing it to the LoadDocument method:
stream.Seek(0, SeekOrigin.Begin)
Arun
Top achievements
Rank 2
Iron
commented on 19 Jun 2022, 05:04 AM | edited

Hi Mr Demirev,

Thank you very much for your suggestion. It worked by exporting first to the file system and LoadDocument(File Name).

I am still searching for the solution to load from stream direct so that we don't want to store 1st as a file. (

stream.Seek(0, SeekOrigin.Begin)

) this didn't work.  (I May used it wrongly)

Thank you.

Nikolay Demirev
Telerik team
commented on 20 Jun 2022, 12:02 PM

Hello Arun,

It would be easier for me to help you if you set up a sample project I could use to reproduce the issue. Could you please create a sample project maybe using for example SQL Line in-memory database? This way I will be able to run it and investigate the issue on my end.

As for my suggestion for stream.Seek, you could use the following code:

 Dim eXactConnection As New SqlClient.SqlConnection(ConnStr)
        Dim eXactCommand As New SqlClient.SqlCommand("SELECT LYR_Doc from tbl_Doc where LYR_Name =  'Arun'", eXactConnection)
        eXactConnection.Open()
        If Not IsDBNull(eXactCommand.ExecuteScalar()) Then
            Dim Docdata As Byte() = DirectCast(eXactCommand.ExecuteScalar(), Byte())
            Using stream As New IO.MemoryStream(Docdata)
stream.Seek(0, SeekOrigin.Begin) RadPdfViewer1.LoadDocument(stream) End Using End If

Arun
Top achievements
Rank 2
Iron
commented on 06 Jun 2023, 04:36 AM

Mr Demirev, 

Thank you very much. It worked.

No answers yet. Maybe you can help?

Tags
PdfViewer and PdfViewerNavigator
Asked by
Arun
Top achievements
Rank 2
Iron
Share this question
or