Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to read from ms sql file to adobe PDF reader
Error is line 8
 
Error CS0029 Cannot implicitly convert type 'System.IO.MemoryStream' to 'string'
 
Some help?


What I have tried:

try  
                {  
                    if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)  
                    {  
                        byte[] ap = (byte[])documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value;  
                        MemoryStream ms = new MemoryStream(ap);  
                       
                        axAcroPDF1.src = ms;  //here is error
  
  
                          
                    }  
                    else {  
                        axAcroPDF1.src = null;  
                    }  
                }  
                catch  
                {  
                    axAcroPDF1.src = null;  
                }  
Posted
Updated 19-Jul-18 4:33am
Comments
Patrice T 18-Jul-18 17:53pm    
What are you trying to do?
What you don't understand in the error message ?
Goran Bibic 19-Jul-18 1:41am    
I want to read file from database

datagrid selection changed

try
{
if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)
{
byte[] ap = (byte[])documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value;
MemoryStream ms = new MemoryStream(ap);
//dataPictureBox.Image = System.Drawing.Image.FromStream(ms);

axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
}
else {
axAcroPDF1.src = null;
}
}
catch
{
axAcroPDF1.src = null;
}

As Gerry said, the control you're using doesn't support loading files from a stream. You have to save them to disk first.
C#
byte[] ap = (byte[])documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value;
string tempName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".pdf");
File.WriteAllBytes(tempName, ap);
axAcroPDF1.src = tempName;
NB: You'll need some code to delete the temporary file when you've finished with it, or when your application closes.

If you want a control that supports loading directly from the stream, try PdfiumViewer[^], which uses Google's Pdfium library[^]. If you install it via NuGet, pay attention to the installation instructions[^] - you'll need one or more additional packages to install the native Pdfium library.
 
Share this answer
 
Comments
Goran Bibic 20-Jul-18 2:35am    
I have location of file every time when upload pdf. like

D:\SPEDICIJA MERIDIJAN\informacija\SKM2556.pdf

This is in sql database inserted when upload file.

Maybe to use that?

Instead this string tempName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".pdf");

Column name in sql is file_location
Richard Deeming 20-Jul-18 7:20am    
If that file location is valid on every computer where your application is running, then that would work.

Typically, you would use a UNC path pointing to a shared folder on a server on the local network for that. Or a path pointing to a mapped network drive that's mapped by all computers running your code.
Goran Bibic 20-Jul-18 2:38am    
Error is when open my app
File is not started with '%PDF-'
file:///C|/Users/PC/AppData/Local/Temp/ozcbqyu.zmu.pdf
Richard Deeming 20-Jul-18 7:21am    
That error suggests that the data in your database is not a valid PDF file.
Goran Bibic 23-Jul-18 1:31am    
I use this code for insert pdf file...

byte[] filedata = null;
MemoryStream ms = new MemoryStream();
filedata = ms.GetBuffer();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());

insert into dbo.documents (pdf_file) VALUES (@pdf_files)
querySaveStaff.Parameters.AddWithValue("@pdf_file",SqlDbType.Binary).Value=filedata;

Is this way ok?
It's (probably) asking for the "path to a PDF file" (and not a "stream");

Copy the stream to a disk file, and supply the "file path" string (where you got your error).
 
Share this answer
 
Comments
Goran Bibic 19-Jul-18 1:40am    
But I dont have path location....I just want to read file from database
Simon_Whale 19-Jul-18 5:49am    
what you need to do is read the blob from the database and save it temporarily to file on the local machine and then load the pdf in the normal way.
Goran Bibic 19-Jul-18 8:28am    
To find from some location? Folder some exmpl...? Just way from database? I think it is ok solution to insert in database like varbinary(MAX).

That is bad solution?
Goran Bibic 20-Jul-18 4:08am    
Maybe some solution with data reader?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900