Goran Bibic

Goran Bibic

  • 484
  • 2.9k
  • 196.8k

Retrieve pdf file from sql c#

Jul 31 2018 1:39 AM
Some help please
 
Insert into sql
 
  1. byte[] filedata = null;  
  2.            MemoryStream ms = new MemoryStream();  
  3.            filedata = ms.GetBuffer();  
  4.            axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());  
  5.   
  6.   
  7.                    using (SqlConnection openCon = new SqlConnection(cs))  
  8.                    {  
  9.   
  10.                        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number, file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";  
  11.   
  12.                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  13.                        {  
  14.                            querySaveStaff.Connection = openCon;                            
  15.                            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;  
  16.                            querySaveStaff.Parameters.AddWithValue("@pdf_file",SqlDbType.VarBinary).Value=filedata;  
  17.   
  18.                            openCon.Open();  
  19.                            querySaveStaff.ExecuteNonQuery();  
  20.                            openCon.Close();  
  21.   
  22.                              
  23.   
  24.                        }  
  25.   
  26.                    }  
  27.                } 
 
 In Selection change datagrid put this
 

  1. try  
  2.                 {  
  3.                      if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)  
  4.                      {  
  5.                         byte[] ap = (byte[])documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value;  
  6.                         MemoryStream ms = new MemoryStream(ap);  
  7.                         axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());  
  8.                           
  9.                     }  
  10.                     else {  
  11.                          axAcroPDF1.src = null;  
  12.                      }  
  13.                 }  
  14.                 catch  
  15.                 {  
  16.                     axAcroPDF1.src = null;  
  17.                 } 
 Error is when retrieve file...(no have error) noting hapend....what I wrong?
 
I think at line 3 to line 9
 

Answers (7)