hei, im new around here, and i tried to display a image from sqlserver
i didnt get error but the code dont display the image...
thanks

default.aspx

protected void Page_Load(object sender, EventArgs e)
    {
string sql = string.Format("SELECT TOP 1 ItemId,Thumb FROM RubrikkImg.dbo.Attachment");
            List<object[]> img = Db.GetData(sql);
            lblResult.Text = String.Format("ItemId is {0} - \n Thumb Data ", img[0][0].ToString());//,BitConverter.ToString((byte[])img[0][1]));
            // Display the image from the database
            Image1.ImageUrl = "~/ImgHandler.ashx?ItemId=" + img[0][0].ToString();
}

ImgHandler.ashx

public class ImgHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string empno;
        if (context.Request.QueryString["ItemId"] != null)
            empno = context.Request.QueryString["ItemId"].ToString();
        else
            throw new ArgumentException("No parameter specified");
        
        context.Response.ContentType = "image/jpeg";
        context.Response.ContentType = "image/bmp";
        string sql = string.Format("SELECT Thumb FROM RubrikkImg.dbo.Attachment WHERE ItemId = '{0}'", empno);
        List<object[]> img = Db.GetData(sql);
        
            byte[] imgBytes = (byte[])img[0][0];
            context.Response.BinaryWrite(imgBytes);


    }
}
public class ImgHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string empno;
        if (context.Request.QueryString["ItemId"] != null)
            empno = context.Request.QueryString["ItemId"].ToString();
        else
            throw new ArgumentException("No parameter specified");
        
        context.Response.ContentType = "image/png";

        string sql = string.Format("SELECT Thumb FROM RubrikkImg.dbo.Attachment WHERE ItemId = '{0}'", empno);
        List<object[]> img = Db.GetData(sql);
        
        byte[] imgBytes = (byte[])img[0][0];
        context.Response.BinaryWrite(imgBytes);
        context.Response.Flush();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.