Hi everyone,
i'm trying to save PDF file into SQL DB.
I don't get any error, but PDF is not saved in data table.
Here is my code:
-
- public void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog openfd = new OpenFileDialog();
- openfd.Filter = "PDF Files (*.pdf)|*.pdf";
- if (openfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- axAcroPDF1.src = openfd.FileName;
- textBox1.Text = openfd.FileName;
- }
- else
- { }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- if(textBox1.Text != null)
- try
- {
- byte[] pdfb = null;
- FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
- BinaryReader br = new BinaryReader(fs);
- pdfb = br.ReadBytes((int)fs.Length);
- SqlConnection cn = new SqlConnection(@"Data Source=MAIN-PC\SQLEXPRESS;Database = " + comboBox1.Text + ";Integrated Security=True");
- string query = "Insert into tblInvoices (StoreCode,Supplier,RID,Status)values ('" + comboBox2.Text + "','" + comboBox3.Text + "','" + DateTime.Now.ToString()+ "', 'Received')";
- SqlCommand command = new SqlCommand(query, cn);
- command.Parameters.Add("@InvoicePDF", SqlDbType.VarBinary, 8000).Value = pdfb;
- cn.Open();
- command.ExecuteNonQuery();
- cn.Close();
- MessageBox.Show("Invoice uploaded successfully", MessageBoxIcon.Information.ToString(), MessageBoxButtons.OK );
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
SQL DB,Table config is:
StoreCode varchar(50) Checked
Supplier nvarchar(50) Unchecked
RID date Unchecked
CID date Checked
Status nvarchar(50) Unchecked
InvoicePDF varbinary(MAX) Checked
Managed nvarchar(50) Checked
Unchecked