Goran Bibic

Goran Bibic

  • 479
  • 2.9k
  • 197.9k

Retrieve pdf file from db c#

Aug 6 2018 12:58 PM
Insert into working fine
 
  1. private void Button2_Click(object sender, EventArgs e)   //Save   //snimi  
  2.        {             
  3.   
  4.            OpenFileDialog ofd = new OpenFileDialog() { Filter = "PDF|*.pdf" };//Open pdf file  
  5.            if (ofd.ShowDialog() == DialogResult.OK)  
  6.            {  
  7.   
  8.                FileStream fs = File.OpenRead(ofd.FileName);  
  9.                MemoryStream ms = new MemoryStream();  
  10.                axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());  
  11.                fs.CopyTo(ms);  
  12.   
  13.            if (count_numberTextBox.Enabled == true)  
  14.            {  
  15.                if (string.IsNullOrEmpty(idTextBox.Text))  
  16.                {  
  17.   
  18.                    using (SqlConnection openCon = new SqlConnection(cs))  
  19.                    {  
  20.   
  21.                        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number, count_number, label, partner, tax_number, date, file_location, pdf_file) VALUES (@maxNo,@count_number,@label,@partner,@tax_number,@date,@file_location,@pdf_file)";  
  22.   
  23.                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  24.                        {  
  25.                            querySaveStaff.Connection = openCon;  
  26.                            querySaveStaff.Parameters.Add("@count_number", SqlDbType.VarChar, 255).Value = count_numberTextBox.Text;  
  27.                            querySaveStaff.Parameters.Add("@label", SqlDbType.VarChar, 255).Value = labelTextBox.Text;  
  28.                            querySaveStaff.Parameters.Add("@partner", SqlDbType.VarChar, 255).Value = partnerComboBox.Text;  
  29.                            querySaveStaff.Parameters.Add("@tax_number", SqlDbType.VarChar, 255).Value = tax_numberTextBox.Text;  
  30.                            querySaveStaff.Parameters.Add("@date", SqlDbType.Date).Value = dateDateTimePicker.Text;  
  31.                            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;                              
  32.                            querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = ms.ToArray();  
  33.   
  34.                            openCon.Open();  
  35.                            querySaveStaff.ExecuteNonQuery();  
  36.                            openCon.Close();  
  37.   
  38.                            dateDateTimePicker.Enabled = false;  
  39.                            count_numberTextBox.Enabled = false;  
  40.                            file_locationTextBox.Enabled = false;  
  41.                            labelTextBox.Enabled = false;  
  42.                            partnerComboBox.Enabled = false;  
  43.                            tax_numberTextBox.Enabled = false;  
  44.                            button3.Enabled = false;  
  45.                            button4.Enabled = false;  
  46.   
  47.   
  48.                        }  
  49.   
  50.                    }  
  51.                }  
  52.                else  
  53.                {  
  54.   
  55.                    using (SqlConnection openCon = new SqlConnection(cs))  
  56.                    {  
  57.                        string saveStaff = "UPDATE dbo.documents SET count_number=@count_number, label=@label, partner=@partner, tax_number=@tax_number, date=@date, file_location=@file_location, pdf_file=@pdf_file   WHERE id= " + idTextBox.Text;  
  58.   
  59.                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  60.                        {  
  61.   
  62.                            querySaveStaff.Connection = openCon;  
  63.                            querySaveStaff.Parameters.Add("@count_number", SqlDbType.VarChar, 255).Value = count_numberTextBox.Text;  
  64.                            querySaveStaff.Parameters.Add("@label", SqlDbType.VarChar, 255).Value = labelTextBox.Text;  
  65.                            querySaveStaff.Parameters.Add("@partner", SqlDbType.VarChar, 255).Value = partnerComboBox.Text;  
  66.                            querySaveStaff.Parameters.Add("@tax_number", SqlDbType.VarChar, 255).Value = tax_numberTextBox.Text;  
  67.                            querySaveStaff.Parameters.Add("@date", SqlDbType.Date).Value = dateDateTimePicker.Text;  
  68.                            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;  
  69.                            //querySaveStaff.Parameters.Add("@pdf_file", SqlDbType.VarChar, 255).Value = partner_typeComboBox.Text;  
  70.                            querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = ms.ToArray();  
  71.   
  72.                                openCon.Open();  
  73.                            querySaveStaff.ExecuteNonQuery();  
  74.                            MessageBox.Show("Uspješno ste izmenili stavku!""Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  75.                            openCon.Close();  
  76.   
  77.                            dateDateTimePicker.Enabled = false;  
  78.                            count_numberTextBox.Enabled = false;  
  79.                            file_locationTextBox.Enabled = false;  
  80.                            labelTextBox.Enabled = false;  
  81.                            partnerComboBox.Enabled = false;  
  82.                            tax_numberTextBox.Enabled = false;  
  83.                            button3.Enabled = false;  
  84.                            button4.Enabled = false;  
  85.   
  86.                        }  
  87.   
  88.                    }  
  89.                }  
  90.            }  
  91.   
  92.            else  
  93.   
  94.            {  
  95.                MessageBox.Show("Dokument je vec potvrden! Unesite novi ili izmjenite postojeci!""Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  96.   
  97.            }  
  98.   
  99.            using (SqlConnection openCon = new SqlConnection(cs))  
  100.   
  101.            {  
  102.   
  103.                string saveStaff = "SELECT * FROM dbo.documents ";  
  104.                openCon.Open();  
  105.                using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))  
  106.                {  
  107.                    querySaveStaff.Fill(dMS_2018DataSet.documents);  
  108.                    documentsDataGridView.DataSource = dMS_2018DataSet.documents;  
  109.                    documentsDataGridView.Update();  
  110.                    documentsDataGridView.Refresh();  
  111.                }  
  112.   
  113.            }  
  114.   
  115.            countRowsLabel.Text = "Ukupno redova: " + documentsDataGridView.RowCount.ToString();  
  116.        }  
  117.   
  118.    } 
 
For retrieve file I chose this
 
  1. private void DocumentsDataGridView_SelectionChanged(object sender, EventArgs e)  
  2.        {  
  3.            if (documentsDataGridView.SelectedRows.Count > 0)  
  4.            {  
  5.   
  6.                string id = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn1"].Value.ToString();  
  7.                string number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn2"].Value.ToString();  
  8.                string count_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn3"].Value.ToString();  
  9.                string label = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn4"].Value.ToString();  
  10.                string partner = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn5"].Value.ToString();  
  11.                string tax_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn6"].Value.ToString();  
  12.                string file_location = documentsDataGridView.SelectedRows[0].Cells["file_location"].Value.ToString();  
  13.                 
  14.   
  15.   
  16.                idTextBox.Text = id;  
  17.                numberTextBox.Text = number;  
  18.                count_numberTextBox.Text = count_number;  
  19.                labelTextBox.Text = label;  
  20.                partnerComboBox.Text = partner;  
  21.                tax_numberTextBox.Text = tax_number;  
  22.                dateDateTimePicker.Value = Convert.ToDateTime(documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn7"].Value);  
  23.                file_locationTextBox.Text = file_location;  
  24.                 
  25.   
  26.                try  
  27.                {  
  28.                    if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)  
  29.   
  30.                    {  
  31. //////////////////////////////here need help  
  32. }  
  33.                    else  
  34.                    {  
  35.                        axAcroPDF1.src = null;  
  36.                    }  
  37.                }  
  38.                catch  
  39.                {  
  40.                    axAcroPDF1.src = null;  
  41.                }  
  42.   
  43.            }  
  44.        } 
 

Answers (1)