Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET 2.0 Cache binary data between pages

Status
Not open for further replies.

lastdonuk

Programmer
Jan 19, 2005
58
GB
Hi,

My issue is that a cached object called from a new page does not display until you refresh the new page (C#).

I have a requirement to pass an image held in an SQL image datatype field into a new window when produced from a button onClick event.

The initial creation of the image is fine, and here's the button HTML using Javascript to force a new page:

Code:
<asp:ImageButton ID="Photo" BorderStyle="None" runat="server" ImageUrl="~/images/photo.gif" OnClick="Photo_Click" OnClientClick="javascript:popUpWin('PhotoPopUp.aspx','console',525,525);" />

The Caching namespace is declared in the .cs file:

Code:
using System.Web.Caching;

And the final part of the onClick event in the Code Behind passes the data into a cache object:

Code:
conn2.Open();
SqlDataReader dr = cmd.ExecuteReader();   
dr.Read();       
byte[] bindata = (byte[])dr.GetValue(0);
Cache["Bindata"] = bindata;
conn2.Close();

And here's the Code Behind of the new page I do this to retrieve the image from the cache and display it to the user:

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using InfoSoftGlobal;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using DataConnection;
using System.Web.Caching;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

public partial class customers_PhotoPopUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        byte[] i_photo;

        if (Cache["Bindata"] == null)
        {
            noimage.Visible = true;
        }
        else
        {
            i_photo = (byte[])Cache["Bindata"];

            Response.AddHeader("Content-Type", "image/pjpeg");
            Response.BinaryWrite(i_photo);
        }
    }
}

So, this is all good except that the user doesn't see the image until they refresh the page...I'm not currently explicitly adding any Cache instructions to the page, should I?

Can I force a refresh or am I just using Cache in error?

If I thought I could use a QueryString, I would, but as I'm dealing with Binary data, I'm a bit unsure of my options...

Any help/thoughts gratefully received.

lastdonuk
 
Have you considered putting the byte into a Session object instead? All the code should work exactly as you have it written just change "Cache" to "Session". I have found session objects behave more as expected than cache.

The next question is when you bebug this code on the first appearance of the new page what is the value of Cache["Binddata"]. Is that value null before the refresh?
 
Hi evaleah,

I did briefly try a Session, but besides replacing "Cache" with "Session" do I explicitly need to turn Sessions on or anything?

In answer to your second question, yes, the bindata object is null before refreshing...

Thanks for the response,

Rob

lastdonuk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top