Displaying Picture from NorthWind Categories Table RRS feed

  • Question

  • User736763810 posted

    Hi all,

    I've spent half the day trying to figure out what I am doing wrong in my attempt to display the image data from the Northwind Database's Categories relation (MS Access version). Here is what I have done:

    1. created a httphandler called ImageFromNwDb.ashx  <%@ WebHandler Language="C#" Class="ImageFromNwDb" %>

    2. created the class for that handler:

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.Configuration;
    /// &lt;summary&gt;
    /// Summary description for ImageFromNwDb
    /// </summary>
    public class ImageFromNwDb : IHttpHandler
        bool IHttpHandler.IsReusable
            get { return true; }
        void IHttpHandler.ProcessRequest(HttpContext context)
            String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + context.Server.MapPath("~/App_Data/Northwind.mdb");
            // Get the ID for this request.
            String ID = context.Request.QueryString["catID"].ToString();
            if (ID == null)
                throw new ApplicationException("Must specify a category ID");
            // Create a parameterized command for this record.
            OleDbConnection con = new OleDbConnection(connString);
            string SQL = "SELECT Picture FROM Categories " +
                            "WHERE CategoryID = " + ID + ";";
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            //cmd.Parameters.AddWithValue("?", ID);
                OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                if (reader.Read())
                    int bufferSize = 100;                   // Size of the buffer.
                    byte[] bytes = new byte[bufferSize];    // The buffer.
                    long bytesRead;                         // The # of bytes read.
                    long readFrom = 0;                      // The starting index.
                        // Read the field 100 bytes at a time.
                        bytesRead = reader.GetBytes(0, readFrom, bytes, 0, bufferSize);
                        // 0 - the zero-based column returned from the SQL command
                        // readFrom - the index within the field from which to commence the read operation
                        // bytes - the buffer to read the bytes into
                        // 0 -  the index within the buffer from where the write operation is to commence
                        // bufferSize - the maximum length to copy into the buffer
                        /*  don't forget to update web.config:
                         * <add verb="GET" path="ImageFromAWDB.ashx" type="ImageFromDBHandler"/>
                        readFrom += bufferSize;
                    } while (bytesRead == bufferSize);
    3. added the handler to web.config inside the <httpHandlers> element:

    <add verb="GET" path="ImageFromNwDb.ashx" type="ImageFromNwDb, App_Code" />

    4. added a GridView and AccessDataSource:

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            SelectCommand="SELECT Products.ProductName, Suppliers.CompanyName, Suppliers.ContactName, Categories.CategoryName, Categories.Description, Categories.Picture, Categories.CategoryID FROM ((Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID) INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID)">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" 
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                    SortExpression="ProductName" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                    SortExpression="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" 
                    SortExpression="ContactName" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:TemplateField HeaderText="Logo">
                        <img src='ImageFromNwDb.ashx?catID=<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>' alt="Category Logo" />

    Unfortunately, it is not working. I am just getting the browser placeholder image + the alt text in the img tag.

    I'm sure there is some small thing that I am missing. Any help would be great.


    Monday, March 23, 2009 1:30 AM

All replies

  • User129324971 posted


    Because access, you need to remove 78 bytes from the image.



    Sunday, April 17, 2011 10:26 AM