0

I want to retrieve image from database and display in an aspx page. I use Linq to SQL. And a Generic handler.

Handler2.ashx code:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.QueryString["id"] != null)
    {
        int id;
        string sid = context.Request.QueryString["id"];
        if (int.TryParse(sid, out id))
        {
            Stream strm = getImage(id);
            byte[] buffer = new byte[4096];
            int i = strm.Read(buffer, 0, 4096);
            while (i > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, 4096);
                i = strm.Read(buffer, 0, 4096);
            }
        }
        else
        {
            //
        }

    }
}


public Stream getImage(int id)
{
    using (DummyDBEntities cntx = new DummyDBEntities())
    {
        var db = from c in cntx.Images
                    where c.imageId == id
                    select c.imageData;
        MemoryStream ms = new MemoryStream();
        byte[] byteArray = new byte[db.ToArray().Length];
        ms.Write(byteArray, 0, db.ToArray().Length);

        return new MemoryStream(byteArray);

    }
}

And a button control in Default.aspx page when I click, redirects to handler1.ashx. Gets the id of image from database and supposed to Show it in Default.aspx asp:image control

protected void btnGetID_Click(object sender, EventArgs e)
{
  int id=Convert.ToInt32(txtGetID.Text);
  Response.Redirect("Handler2.ashx?id="+id);
  Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';
}

How do i supposed to write Eval method and the querystring to pass the image to imageurl?

Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';

Please help, thanks.

Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14
perlynsparks
  • 401
  • 3
  • 7
  • 20
  • If you're redirecting, would you even be able to see the updated Image1? – Smeegs Jul 09 '14 at 13:41
  • `Response.Redirect` will redirect the browser to an entirely different page. So Image1 won't be visible. – Smeegs Jul 09 '14 at 14:21
  • First it redirects to Handler2.ashx page, does some stream conversions to byte and then continues the code from back here: Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>'; – perlynsparks Jul 09 '14 at 14:43
  • You should read up on `Response.Redirect` http://msdn.microsoft.com/en-us/library/ms524309(v=vs.90).aspx Especially this part `code execution in the current page is terminated when the Redirect method is processed, so subsequent code in the page will also be ignored.` – Smeegs Jul 09 '14 at 14:45
  • `Response.Redirect` Redirects the browser, you won't be at `default.aspx` anymore, it will just display the image on the screen and nothing else. – Smeegs Jul 09 '14 at 14:48
  • Sorry, just got what you mean! Redirecting to handler page is pointless here really.. I just wanted to pass id to handler by using querystring. May be i should use session or sth? I mean without redirecting to handler.. – perlynsparks Jul 09 '14 at 19:31
  • I gotta go with the proposed solution on this. If you remove the redirect and add the code (assuming that `id` is what you want to pass to the query string), you should be fine. – Smeegs Jul 09 '14 at 19:36
  • Sure, but do i need to use here POST method then? Without redirecting – perlynsparks Jul 09 '14 at 20:07

1 Answers1

0

I hope I understood your problem right. You want to display the image from your handler in the Image1. For that you can just do the following

Image1.ImageUrl = ResolveUrl("~/Handler2.ashx?id=" + id);
Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14
  • Actually this type of code only redirects to Handler1.ashx page. What i want to do is; after getting the id of a specific image, to display image in the Default.aspx, inside the control.. – perlynsparks Jul 09 '14 at 14:15
  • I think i should pass the image column data (id, and imageData)via querystring to Default.aspx, and also use some Eval() method to display image in asp:image control. But I dont know how to do.. – perlynsparks Jul 09 '14 at 14:18
  • I am sorry, why do you want to redirect to handler1.ashx or handler2.ashx? If you just assign the url to the image1, then it takes care of retrieving the image from the url, because your ashx writes binary data to the response. – Kiran Hegde Jul 09 '14 at 14:21
  • It's the first time i use a Generic handler. I ve been searching for to retrieve image from db and display in an asp control. And I followed this tutorial: http://www.dotnetcurry.com/showarticle.aspx?ID=129. Thats why i did just like that. So, do you mean that i can do it without using a handler? – perlynsparks Jul 09 '14 at 14:40
  • The handler is fine. Please follow according to the tutorial. You have written the handler which accepts id as query string parameter, retrieves the image from database and writes the image to the response. To display the image in the image control from this handler you need to just assign the ImageUrl property of the image control to this handler with querystring parameter – Kiran Hegde Jul 09 '14 at 14:46