Greetings everyone i am trying to insert an image from my C# form application to MySql table but i don't know how to deal with blob format help is apricated
private void button1_Click(object sender, EventArgs e)
{
try
{
Con.Open();
MySqlCommand cmd = new MySqlCommand("insert into ProductTable values('" + prodidTB.Text + "','" + bunifuPictureBox1.Image + "','" + suppliertxt.Text + "','" + prodnameTB.Text + "','" + prodqtyTB.Text + "','" + prodpriceTB.Text + "','" + proddescTB.Text + "','" + catcombo.SelectedValue.ToString() + "')", Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Product Succesfully Added");
Con.Close();
populate();
proddescTB.Text = "";
prodidTB.Text = "";
suppliertxt.Text = "";
prodqtyTB.Text = "";
prodnameTB.Text = "";
prodpriceTB.Text = "";
bunifuPictureBox1.Image = null;
}
catch
{
}
}
that's it for the code The populate method i am using
void populate()
{
try
{
Con.Open();
string Myquery = "select * from ProductTable";
MySqlDataAdapter da = new MySqlDataAdapter(Myquery, Con);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
ProductsGv.DataSource = ds.Tables[0];
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn = (DataGridViewImageColumn)ProductsGv.Columns[1];
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
ProductsGv.Columns[6].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
Con.Close();
}
catch
{
}
}
that's it for the method populate
As I understand, you need to save the image from the PictureBox
to the database.
If you are saving an image that was created in a PictureBox
then use this:
MemoryStream ms = new MemoryStream();
bunifuPictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); // You can use other format
If the image in the PictureBox
was loaded from a file, then:
MemoryStream ms = new MemoryStream();
Image image = Image.FromFile("Image.jpg");
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
After that, in the insert query into the table, in place of the image parameter, use:
ms.ToArray()
And don't forget to Close()
MemoryStream
after insert.