Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my code for inserting image from picturebox into database.
Image img = this.pictureBox1.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
I then insert arr into the database.

This is the code I use to select the image from database into a picturebox. But it gives me an error"Parameter is not valid."

string query = "SELECT * FROM Drug WHERE DrugCode = '" + this.DataGridView1.CurrentRow.Cells[0].Value + "'";
SqlCommand cmd = new SqlCommand(query, Conet.con);
SqlDataReader row = cmd.ExecuteReader();

Form_EditDrug newMDIChild = new Form_EditDrug(this.txtStaff.Text);
newMDIChild.TopMost = true;
newMDIChild.BringToFront();
if (row.Read())
{
byte[] data = (byte[])row["Picture"];
MemoryStream ms = new MemoryStream(data);

newMDIChild.pictureBox1.Image = Image.FromStream(ms);
}

Help please.
Posted

1 solution

See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

And please, don't do database access that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900