ASP.NET Dynamic Data - Displaying, Inserting and Editing Images in SQL Server Database

ASP.NET Dynamic Data - Displaying, Inserting and Editing Images in SQL Server Database

by David Hayden ( Florida ASP.NET Developer ), Filed: ASP.NET 3.5

 

You can get up to speed on ASP.NET Dynamic Data in the ASP.NET 3.5 Extensions CTP by reading the following tutorials:

 

Displaying, Inserting and Editing Images in SQL Server Database

Scott Hunter published a really cool sample of ASP.NET Dynamic Data where he adds functionality to Dynamic Data that allows one to display, insert, and edit images from a SQL Server Database. Here is a snapshot of his example that uses the Northwind Database:

 

Dynamic Data Images

 

 

Scott's example shows off a number of really cool features and techniques:

  • Authoring Custom Dynamic Data Fields: DbImage.ascx, DbImage_Edit.ascx, and FileImage.ascx.
  • Using a Custom Attribute, ImageFormat, that contains format metadata.
  • Creating a Custom HttpHandler, ImageHandler.ashx, for displaying images from the database.
  • Generating a LINQ query on the fly, via LinqImageHelper, to get the image using LINQ To SQL.
  • Working with various Metadata and Reflection Classes in ASP.NET Dynamic Data and the .NET Framework.

 

When you download his sample, you will see the new custom Dynamic Data Fields of DbImage.ascx, DbImage_Edit.ascx, and FileImage.ascx:

 

Custom Dynamic Data Fields

 

You can learn more about creating custom Dynamic Data Fields at ASP.NET Dynamic Data Websites - Creating Custom Dynamic Data Fields.

There are numerous helper classes in a separate project that consist of the ImageFormat Attribute, ImageHandler HTTP Handler, LINQImageHelper for generating a LINQ To SQL query to get the image, etc.

 

ASP.NET Dynamic Data

 

I really like the use of a ImageFormatAttribute to decorate the LINQ To SQL Class with format metadata that provides the width and height information for which to display the image:

 

[ImageFormat("Picture", 100, 100)]
[RenderHint("Picture", "DbImage")]
public partial class Category 
{
}

 

You can learn more about the RenderHint Attribute at  ASP.NET Dynamic Data Websites - Creating Custom Dynamic Data Fields.

When the sample application is running, I recommend running Fiddler to see the incoming calls to the ImageHandler HTTP Handler to see the querystring data that is generated into a LINQ Query to access the image from SQL Server and display it on the website. Some pretty cool techniques in these classes to work with Dynamic Data Metadata, create DataContext and other classes using Reflection, and cache the image information to save on all that Reflection overhead the first time one requests an image. Here is a sample request to ImageHandler:

 

ImageHandler.ashx
    ?table=Categories
    &column=Picture
    &pkv=1
    &width=100
    &height=100

 

Very, very cool.

 

Conclusion

If you really want to understand how to access much of the Dynamic Data Metadata as well as create custom Dynamic Data Fields, download the sample and play with it. Scott's sample has all kinds of great techniques that will get you thinking about the possibilities of Dynamic Data. Thanks for the sample, Scott!

 

Site: http://www.davidhayden.com/

Author:  David Hayden ( Florida ASP.NET Developer )

Tag: ASP.NET 3.5

 

posted on Tuesday, January 15, 2008 9:00 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices