Read PDF file from SQL to AxacroPDF C#

  • Thread starter Thread starter Goranbss
  • Start date Start date
G

Goranbss

Guest
Insert into

byte[] 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, count_number, label, partner, tax_number, date, file_location, pdf_file) VALUES (@maxNo,@count_number,@label,@partner,@tax_number,@date,@file_location,@pdf_file)";

using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;
querySaveStaff.Parameters.Add("@count_number", SqlDbType.VarChar, 255).Value = count_numberTextBox.Text;
querySaveStaff.Parameters.Add("@label", SqlDbType.VarChar, 255).Value = labelTextBox.Text;
querySaveStaff.Parameters.Add("@partner", SqlDbType.VarChar, 255).Value = partnerComboBox.Text;
querySaveStaff.Parameters.Add("@tax_number", SqlDbType.VarChar, 255).Value = tax_numberTextBox.Text;
querySaveStaff.Parameters.Add("@date", SqlDbType.Date).Value = dateDateTimePicker.Text;
querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;
//querySaveStaff.Parameters.Add("@pdf_file", SqlDbType.VarChar, 255).Value = partner_typeComboBox.Text;
querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;

openCon.Open();
querySaveStaff.ExecuteNonQuery();
openCon.Close();

dateDateTimePicker.Enabled = false;
count_numberTextBox.Enabled = false;
file_locationTextBox.Enabled = false;
labelTextBox.Enabled = false;
partnerComboBox.Enabled = false;
tax_numberTextBox.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;


}

}

Retrieve from sql selection changeddatagrid


if (documentsDataGridView.SelectedRows.Count > 0)
{

string id = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn1"].Value.ToString();
string number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn2"].Value.ToString();
string count_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn3"].Value.ToString();
string label = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn4"].Value.ToString();
string partner = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn5"].Value.ToString();
string tax_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn6"].Value.ToString();
string file_location = documentsDataGridView.SelectedRows[0].Cells["file_location"].Value.ToString();
//string pdf_file = documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value.ToString();


idTextBox.Text = id;
numberTextBox.Text = number;
count_numberTextBox.Text = count_number;
labelTextBox.Text = label;
partnerComboBox.Text = partner;
tax_numberTextBox.Text = tax_number;
dateDateTimePicker.Value = Convert.ToDateTime(documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn7"].Value);
file_locationTextBox.Text = file_location;
//axAcroPDF1.src = pdf_file;



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());
//axAcroPDF1.LoadFile(ms);


}
else
{
axAcroPDF1.src = null;
}
}
catch
{
axAcroPDF1.src = null;
}


}

Insert into working fine

varbinary(MAX) is format of data in sql for pdf

Not read pdf file from sql database...some help?

Continue reading...
 
Back
Top