0

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


    }
}
2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by __avd
0
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();
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.