views:

437

answers:

3

I have a sql server database that returns byte for the image. If I use the tableadapter wizard and set it to my stored procedure and preview data, it pulls back an image. It automatically turns it into an image in the preview data. I don't see it as a string of Ints or anything.

How can I display it on my asp.net webpage with a gridview and objectdatasource?

I have searched and foudn where the imagefield can point to a url on another page that does the byte transformation but I'm not sure it's the best. I found another way that creates a temp file.

Just trying to see the best way to do it.

edit - I am trying not to use a temp file. If I cannot use a gridview a regular image field is ok.

asp.net 2.0, c#.

Thank you for any help.

edit

ended up with:

   protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request["id"];
        string connstr = "DSN=myserver";
        OdbcConnection conn = new OdbcConnection(connstr);
        OdbcCommand cmd = new OdbcCommand("{call mySP (?)}", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        OdbcParameter parameter = new OdbcParameter();
        parameter.ParameterName = "@MyParam";
        parameter.OdbcType = OdbcType.VarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = id;

        // Add the parameter to the Parameters collection. 
        cmd.Parameters.Add(parameter);
        conn.Open();
        OdbcDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            byte[] buffer = (byte[])dr[0];
            Response.ContentType = "image/jpg";
            Response.BinaryWrite(buffer);
            Response.Flush();
        }
    }

and this on the calling page:

<asp:Image ID="Image1" ImageAlign="Middle" ImageUrl="show.aspx?id=123" Runat="server" />
A: 

Here is some example code on how to do this.

Chris Ballance
+3  A: 

Two options:

Create a temp file - The problem with this approach is that you have to create the file, which means your web must have write access to a directory which is not a great thing. You also need to have a way to clean up the images.

Serve it from another URL - This is my preferred method, as you have no disk access required. A simple http handler (ashx) is a great method to serve up the image.

Edit

If you need session state in the ashx, check out: http://stackoverflow.com/questions/293276/asp-net-system-web-httpcontext-current-session-null-in-global-asax.

Edit

Couple more thoughts. There are some cases where using a temp file might be better. For example if your images are requested frequently by a lot of users. Then storing the images on the disk would make sense, since you could write the file once, this does increase the maintance complexity but depending on traffic it might be worth it since this would let you avoid calling back into the .net stack and leverage IIS caching of static content.

JoshBerke
+1 I'm all for the ashx handler route.
bendewey
I was hoping not to use these methods. But thank you.
johnny
another ashx vote
Joel Coehoorn
is what I did what you meant by serve it from another url?
johnny
sort of. I would use an ASHX page instead of an aspx page. You avoid the overhead of creating the Page which if called a lot is significant.
JoshBerke
Thank you. I hadn't heard of an ashx page before.
johnny
No problem its a simple way of creating an HTTP Handler
JoshBerke
A: 

the sample code you added is good but you should move it to an ashx file which is meant for such things.

Sruly