Hello
i am trying to retrieve images from database.But my code is not working.Please help me.Thanks
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=company;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select image from image", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) //yup we found our image
{
Response.ContentType = dr["Image"].ToString();
Response.BinaryWrite((byte[])dr["Image"]);
Response.Write(System.Environment.NewLine);
}
Response.Close();
conn.Close();

Jignesh TrivediRank #16 · 50.3K reputationPosted May 15, 2013, 7:54 PM EDT
hi,
I think you must convert your image byte to memory stream object and write with in image object.
please refer
http://www.codeproject.com/Articles/354639/Storing-and-Retrieving-Images-from-SQL-Server-Usin
http://mitch-wheat.blogspot.in/2007/05/save-and-restore-filesimages-to-sql.html
hope this will help you.
Shankar MRank #874 · 2.5K reputationPosted May 15, 2013, 8:12 PM EDT
Please try this link,
http://net-informations.com/csprj/dataset/cs-insert-image.htm
http://net-informations.com/csprj/dataset/cs-retrieve-image.htm
Thanks, Shankar M
Muhammad imran13 reputationPosted May 15, 2013, 9:32 PM EDT
Shankar MRank #874 · 2.5K reputationPosted May 15, 2013, 10:10 PM EDT
I tried it, Its working Fine my dear friend, try this..
Code :
Thanks, Shankar M
Parveen SiwachRank #6,758 · 257 reputationPosted May 15, 2013, 10:26 PM EDT
you can follow this link:
http://www.codeproject.com/Articles/268123/Display-Store-and-Retrieve-Image-Data-from-Databas
This can help you to solve your problem.
Muhammad imran13 reputationPosted May 16, 2013, 12:14 AM EDT
Shankar MRank #874 · 2.5K reputationPosted May 16, 2013, 12:54 AM EDT
The ExecuteScalar() Method here ,
byte[] image = (byte[])command.ExecuteScalar();
just takes only the first column first row of the result set. And here we are converting it to byte[] array type.
Thanks, Shankar M
Muhammad imran13 reputationPosted May 16, 2013, 2:07 AM EDT
Parveen SiwachRank #6,758 · 257 reputationPosted May 16, 2013, 2:28 AM EDT
I am explaining with ListView:
runat="server" />
runat="server" />
In this control: "picAlbum" image control will show all the images based on "Pic_Id" from database.
You have to create an Handler(ashx file) e.g.: ShowImage.ashx to render the image based on Pic_Id.
public class ShowImage : IHttpHandler { public void ProcessRequest(HttpContext context) { Int32 idNo; if (context.Request.QueryString["id"] != null) idNo = Convert.ToInt32(context.Request.QueryString["id"]); else throw new ArgumentException("No parameter specified"); context.Response.ContentType = "image/jpeg"; Stream strm = ShowEmpImage(idNo); byte[] buffer = new byte[4096]; int byteSeq = 0; try { byteSeq = strm.Read(buffer, 0, 4096); } catch (Exception) { return; } while (byteSeq > 0) { context.Response.OutputStream.Write(buffer, 0, byteSeq); byteSeq = strm.Read(buffer, 0, 4096); } } } Earlier shared code also working on same approach. Hope this will help you to solve your problem.Muhammad imran13 reputationPosted May 22, 2013, 9:48 PM EDT