Click here to Skip to main content
15,918,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have found a number of web searches dealing w/ store and retrieve image from db. I have no problem storing image as binary in the db, but am having difficulty binding and displaying on another page. All examples I have found deal w/ upload and view on the same page. IN my case, I am uploading in one page, and viewing in another. My code does not give errors, but the image does not display. Only a box w/ a small "x" in it. I am using a generic handler.

What I have tried:

imagehandler:
C#
public class DisplayImage : IHttpHandler
        {
            string constring = ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString;
            public void ProcessRequest(HttpContext context)
            {
                string ImageID = context.Request.QueryString["Sampleid"];
                SqlConnection conn = new SqlConnection(constring);
                conn.Open();
                SqlCommand cmd = new SqlCommand("select image from solids where sampleid=" + ImageID, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                context.Response.BinaryWrite((Byte[])dr[0]);
                conn.Close();
                context.Response.End();
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }


ASPX page:
   <div>  
    <asp:GridView ID="ImageGrid" runat="server" AutoGenerateColumns="False">  
        <columns>  
        <asp:TemplateField HeaderText="Image">  
        <itemtemplate>  
        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImageID="+ Eval("SampleID") %>' Height="150px" Width="150px"/>  
        </itemtemplate>  
          
        </columns>  
      
</div>  



aspx code behind:
C#
public partial class SolidsDetail : System.Web.UI.Page
    {
        string constring = ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            Image();
        }
        
        private void Image()
        {
            string ImageID = Request.QueryString["Sampleid"];
            SqlConnection conn = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("SELECT * from solids where sampleid =" + ImageID, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ImageGrid.DataSource = dt;
            ImageGrid.DataBind();
        }
    }
Posted
Updated 17-Feb-16 23:06pm
v3
Comments
F-ES Sitecore 16-Feb-16 9:07am    
Post the handler code and the code you're using to generate your img tag.
martyres72 16-Feb-16 13:54pm    
code added
[no name] 16-Feb-16 9:42am    
Provide relevant code which written in other page foe viewing binary image?
martyres72 16-Feb-16 13:54pm    
code added
ZurdoDev 16-Feb-16 10:04am    
How can we help?

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