Biw Mhtm

Biw Mhtm

  • 1.4k
  • 9
  • 6k

My program error ,help me

May 9 2012 4:40 AM
Hi 

i want insert file pdf to database and open file pdf from datagrid.
but i click file from gridview, it  error.
how to?


this my code

 public partial class Form1 : Form
    {
        string _conStr = "Provider=Microsoft.Ace.OleDb.12.0;" + @"Data Source=" + Application.StartupPath + @"\test.accdb";
        public Form1()
        {
            InitializeComponent();
        }


        private void btUpload_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "PDF Files(*.pdf)|*.pdf";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
            }
        }
        public void showdata() {
            OleDbConnection conn = new OleDbConnection(_conStr);


            conn.Open();


            string sql = "SELECT * FROM filestore";
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
            DataSet data = new DataSet();
            adapter.Fill(data, "test");


            dataGridView1.DataSource = data.Tables["test"];


            conn.Close();
        }


        private void btAdd_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection(_conStr);
            conn.Open();




            string sql = "INSERT INTO filestore(name," + "file) VALUES (" + "@name, @file)";
            OleDbCommand cmd = new OleDbCommand(sql, conn);


            cmd = new OleDbCommand(sql, conn);
            cmd.Parameters.AddWithValue("name", textBox1.Text);
            FileStream fiStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);


            BinaryReader binReader = new BinaryReader(fiStream);
            int fiLen = (int)fiStream.Length;
            byte[] file = binReader.ReadBytes(fiLen);
            cmd.Parameters.AddWithValue("@file", file);


            cmd.ExecuteNonQuery();
            conn.Close();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            showdata();
        }










Answers (2)