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();
}
}

Khan Abrar AhmedPosted Jun 17, 2015, 3:53 AM
{
try
{
connection.Open();
string str = "INSERT INTO imageInsertRetrieve (FirstName,LastName,Image) values (@FirstName,@LastName,@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);
string firstname=textBox1.Text;
cmd.Parameters.AddWithValue("@FirstName", firstname);
string LastName=textBox2.Text;
cmd.Parameters.AddWithValue("@LastName", LastName);
cmd.Parameters.AddWithValue("@img", photo_aray);
cmd.ExecuteNonQuery();
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString());
}
finally
{
connection.Close();
}
}
Safayat ZisanPosted Jun 17, 2015, 3:44 AM
Khan Abrar AhmedPosted Jun 17, 2015, 3:21 AM