Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Some help please
 
Insert into sql


yte[] filedata = null;  
           MemoryStream ms = new MemoryStream();  
           filedata = ms.GetBuffer();  
           axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());  
  
  
                   using (SqlConnection openCon = new SqlConnection(cs))  
                   {  
  
                       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)";  
  
                       using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
                       {  
                           querySaveStaff.Connection = openCon;                            
                           querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;  
                           querySaveStaff.Parameters.AddWithValue("@pdf_file",SqlDbType.VarBinary).Value=filedata;  
  
                           openCon.Open();  
                           querySaveStaff.ExecuteNonQuery();  
                           openCon.Close();  
  
                             
  
                       }  
  
                   }  
               }  


What I have tried:

Selection changed datagrid put this code

Error is when retrieve file...(no have error) noting hapend....what I wrong?
 
I think at line 3 to line 9




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 = LocalEncoding.GetString(ms.ToArray());  
                          
                    }  
                    else {  
                         axAcroPDF1.src = null;  
                     }  
                }  
                catch  
                {  
                    axAcroPDF1.src = null;  
                }  
Posted
Updated 30-Jul-18 21:17pm

1 solution

Start by not swallowing your exception: when you use a blank catch, and do nothing useful with it, you throw away all the information you need to identify the problem and solve it.
Do this:
try
{
    ...
}
catch (Exception ex)
{

    ... log the exception, display it in a message box,
    ... or stuff it on your web page - we have no idea
    ... what environment you are working in

    axAcroPDF1.src = null;
}
Then, when the code is run, you can look at the exception and hopefully see what is going wrong. But without that, you are working in the dark...
 
Share this answer
 
Comments
Goran Bibic 31-Jul-18 3:54am    
Idea is:

If have pdf to show him in axAcroPDF1

If dont have axAcroPDF1.src = null;

Some help?
OriginalGriff 31-Jul-18 4:06am    
And what do you think is happening at the moment?
Did you read anything I wrote?
Have you tried using the debugger?
Goran Bibic 31-Jul-18 4:17am    
I read I try...but no have result...I need help to solve that... Thank you
Goran Bibic 2-Aug-18 1:48am    
I try this

byte[] ap = (byte[])(documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value);
MemoryStream ms = new MemoryStream(ap);
ms.Position = 0;
File.WriteAllBytes(@"D:\mypdf.pdf", ms.ToArray());
Process.Start(@"D:\mypdf.pdf");

Dont work
OriginalGriff 2-Aug-18 2:10am    
:sigh:

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

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