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();


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

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



Below is my .aspx code:

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

Any help would be appreciated.

Related posts

Recent Viewed