ASP.NET
  Home arrow ASP.NET arrow Page 4 - Building a Simple Storefront with LINQ
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Building a Simple Storefront with LINQ
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-04-29

    Table of Contents:
  • Building a Simple Storefront with LINQ
  • ProductCategory Table
  • Creating the Master Page
  • Displaying Product Pictures

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Thanks a lot for the article. I was looking for similar example. Waiting for the...
       · Thanks for the comment, and the next article is now posted.Anyway, for everyone...
       · Not sure if I'm missing something obvious. I can't seem t find the link for the part...
       · Sorry, it accidentally got put in the .NET section rather than the ASP.NET section....
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT