Safayat Zisan

Safayat Zisan

  • 956
  • 1.5k
  • 426.9k

Getting exception while inserting image in access database

Jun 16 2015 2:43 PM
I am trying to insert an image to access database through my C# application. But every time I am getting exception
 
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
string str = "INSERT INTO imageInsertRetrieve (FirstName,LastName,Image) values ('" + textBox1.Text + "','" + textBox2.Text + "',(@img))";
cmd = new OleDbCommand(str, connection);
ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@img", photo_aray);
cmd.ExecuteNonQuery();
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString());
}
finally
{
connection.Close();
}
}

Answers (3)