1
Answer

How to retrieve image from database

Jeet gupta

Jeet gupta

12y
1.2k
1
i have insert a image in database but when i retrieving the image from database then i m getting a error that parameter not find. i search the error then find that when the image is converted into bytes and stored into database then it takes 21432 bytes but when i retrieving that image from database then it shows only 13 bytes which are coming from database.

here i am posting my insert and retrieve code.

Code for insert image
------------------------------------
byte[] content = ImageToStream(openFileDialog1.FileName);
                //string s = Encoding.ASCII.GetString(content).ToString();
                byte[] imagebute = content.ToArray();
                string s = (imagebute.Length).ToString();
                con.Open();
                SqlCommand com = new SqlCommand("insert into Table_1 values('" + content + "','" + s + "')", con);


                com.ExecuteNonQuery();
                pictureBox1.Image = null;
                con.Close();--------------------------------------------------------

code for Retreive Image
-------------------------------------------
con.Open();
string sqlStatement="select image from Table_1 where id=" + textBox1.Text + " ";
SqlDataAdapter adapter=new SqlDataAdapter(sqlStatement,con);
DataTable dt=new DataTable("image");
adapter.Fill(dt);

MemoryStream ms=new MemoryStream((byte[])dt.Rows[0]["image"]);
Image image=Image.FromStream(ms,true);
byte[] content = (byte[])(dt.Rows[0]["image"]);
MemoryStream stream = new MemoryStream(content); // getting error here. parameter is not find
pictureBox2.Image = Image.FromStream(stream);
------------------------------------------------------------

is there any solution for this


Answers (1)