I am trying to display an image from the database in an ASP.NET web page. I'm using a generic handler .aspx and .ashx. I have tried to display it but everytime I run it, it displays the broken image icon.

Below is my .ashx code:

public void ProcessRequest(HttpContext context)
            {
                SqlConnection conn = new SqlConnection("DataSource=localhost; Database=varbinary; User ID=****; Password=****");
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                comm.CommandText = "select * from files where FileId=2";
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataTable dt = new DataTable();

                da.Fill(dt);

                byte[] Data = (byte[])dt.Rows[0][1];

                context.Response.ContentType = "image/jpeg";
                context.Response.ContentType = "image/jpg";
                context.Response.ContentType = "image/png";

                context.Response.BinaryWrite(Data);
                context.Response.Flush();

            }

Below is my .aspx code:

<div>
<asp:Image ID="Image1" runat="server" ImageUrl="HandlerImage.ashx?FileId=2" Width="200" Height="200"/>
</div>

Any help would be appreciated.

Related posts

Recent Viewed