Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
i want to show image that save in my database in a picture box .
How do I do it?
Posted
Comments
Basmeh Awad 26-May-13 10:15am    
how did you saved it in database???in Binary??
Meisam Parsa 26-May-13 11:21am    
yes in binary

after executing you datareader and gathering data in your datareader object
C#
SqlDataReader dr = callFaceImage.ExecuteReader();
         if (dr.hasrows())
      { 
                dr.Read(); //you were missing this W/o this command you cannot read data
                byte[] Img =(byte[]) dr["faceImage"];
                MemoryStream ms = new MemoryStream(Img);
                pictureBox3.Image = Image.FromStream(ms);
                pictureBox3.Refresh();
       }
 
Share this answer
 
v3
Comments
Meisam Parsa 26-May-13 11:22am    
this solution not working
Basmeh Awad 26-May-13 11:26am    
what is the error??
what code have you written??
show your code..
Meisam Parsa 27-May-13 2:39am    
Error : Invalid attempt to read when no data is present.


save code :

SqlCommand instoReceiveTbl = new SqlCommand("insert into receiveTable (carNo,enterTime,cardNo,faceImage) values (@carNo,@enterTime,@cardNo,@faceImage)", cnn);
instoReceiveTbl.Parameters.AddWithValue("@cardNo",enterCardNo_txtBox.Text);
instoReceiveTbl.Parameters.AddWithValue("@carNo", "ثبت نشده");
instoReceiveTbl.Parameters.AddWithValue("@enterTime", DateTime.Now);
instoReceiveTbl.Parameters.AddWithValue("@faceImage", imageByte);
instoReceiveTbl.ExecuteNonQuery();


Retrieval code :

SqlCommand callFaceImage = new SqlCommand("select faceImage from receiveTable where cardNo = "+ enterCardNo_txtBox.Text,cnn);
SqlDataReader dr = callFaceImage.ExecuteReader();
byte[] Img =(byte[]) dr["faceImage"];
MemoryStream ms = new MemoryStream(Img);
pictureBox3.Image = Image.FromStream(ms);
Basmeh Awad 27-May-13 2:47am    
now check the solution... you were missing if condition and this command "dr.Read()"; thats why you were getting that error
Error : Invalid attempt to read when no data is present.
Meisam Parsa 27-May-13 3:04am    
thanks buddy ;)
CP has rules[^]! Before you ask a question, you need to use SearchBox (at the right-top corner of this page).

Few articles, which i found using SerachBox:
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]
Store or Save images in SQL Server[^]
Store Images in SQL Server[^]
 
Share this answer
 
Try this method.


C#
cn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM ImageUpload WHERE ImgID='100'",cn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                string image = Convert.ToString(DateTime.Now.ToFileTime());
                byte[] bimage = (byte[])dr["ImgData"];
                FileStream fs = new FileStream(image, FileMode.CreateNew, FileAccess.Write);
                fs.Write(bimage, 0, bimage.Length - 1);
                fs.Close();
                pictureBox1.Image = Image.FromFile(image);
            }
            dr.Close();
            cn.Close();
 
Share this answer
 

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



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