Building a Simple Storefront with LINQ - Displaying Product Pictures
(Page 4 of 4 )
Before we create the home page, we have to create a page that will query the database for a given product's picture and then output that picture to the user. Recall that a binary representation of the picture is stored in the Product table in the ThumbNailPhoto field. Given the ProductID, we need to extract the value of this field and write it out as a bitmap image. This isn't very hard to do.
First, however, we need to set up the proper database classes. Add a new LINQ to SQL classes item to the web site. Name it AdventureWorks.dbml. Then, drag the Product table from the Database Explorer over to the Object Relational Designer (accessed by clicking on AdventureWorks.dbml). You should now see a visual representation of the table:

Now we're ready to get to work. Create a new page called ProductPicture.aspx. Be sure not to select a master page. Of course, what matters in this page is not the markup, but the code in the codebehind file. So, delete everything in ProductPicutre.aspx except for the very first line:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile=
"ProductPicture.aspx.cs"
Inherits="ProductPicture" %>
C# will be doing the lifting here with the Page_Load method. The simplest way to do what we want is like this:
protected void Page_Load(object sender, EventArgs e)
{
AdventureWorksDataContext db = new AdventureWorksDataContext();
var thumbnail = (from p in db.Products
where p.ProductID == int.Parse(Request.QueryString["id"])
select p.ThumbNailPhoto).First();
Response.ContentType = "image/bmp";
Response.BinaryWrite(thumbnail.ToArray());
}
In the above code, we first create an AdventureWorksDataContext object so that we can access the Product table. Then, we run a query, getting the product whose ProductID matches the value of the id in the query string. Note how, at the end of the query, we use First(). This gets the very first result retrieved and returns only that result. Finally, we specify the content type and then write the image out. So, in order to get, for example, the picture associated with a product whose ProductID is 797, we'd access the page like this:
ProductPicture.aspx?id=797
We can further refine the code to only work with a valid ProductID. To do this, we simply need to check that the ProductID is specified in the query string, and then we need to check whether a product with that ProductID does indeed exist:
if (Request.QueryString["id"] != null)
{
AdventureWorksDataContext db = new AdventureWorksDataContext();
var thumbnail = (from p in db.Products
where p.ProductID == int.Parse(Request.QueryString["id"])
select p.ThumbNailPhoto).FirstOrDefault();
if (thumbnail != null)
{
Response.ContentType = "image/bmp";
Response.BinaryWrite(thumbnail.ToArray());
}
}
Accessing the page with an invalid ProductID will now result in a blank page rather than a nasty error message.
Check back next week for the second part of this three-part series!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |