TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Vuk Stanojevic
1.6k
140
45.6k
C# save PDF file in DB
Apr 27 2020 4:30 PM
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:
//select PDF, open/show file and show location in textbox
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
{ }
}
//save/upload button
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
Reply
Answers (
2
)
How to underline keyword in richtextbox
Configure vs2019 community ed. to use c# v8