I'm trying to figure out how to save to db an image. I wrote the following code, but I keep getting this error on line 51, which is cmd.ExecuteNonQuery;. I can't figure out why. The connection ("conn") is already opened before it, but still shows the error. Thanks !
private void button1_Click(object sender, EventArgs e)
{
if (numeTextBox.TextLength != 0 || prenumeTextBox.TextLength != 0)
{
var connString = (@"Data Source= |DataDirectory|\Angajati.sdf");
using (var conn = new SqlCeConnection(connString))
{
try
{
conn.Open();
SqlCeCommand cmd = new SqlCeCommand();
SqlCeParameter picture = new SqlCeParameter("@picture", SqlDbType.Image);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] a = ms.GetBuffer();
ms.Close();
cmd.Parameters.AddWithValue("@picture", a);
cmd.CommandText = "INSERT INTO info(Nume, Prenume,Data, Proiect, Schimburi, Poza) VALUES('" + numeTextBox.Text.Trim() + "', '" + prenumeTextBox.Text.Trim() + "', '" + dateTimePicker1.Text + "', '" + textBox1.Text.Trim() + "', '" + radioButton1.Text + "', @picture);";
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Salvat cu succes!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
else
{
MessageBox.Show("Trebuie sa completezi campurile inainte de a salva!");
}
}