Using LINQ, when working with images in Web Application

In our ASP.NET applications we often need to
create some pages to upload, edit, delete and of course just to see images (this
is: pulling these images out of our database) etc. To do all these operations
with help of LINQ and ObjectDataSourse is simple enough. In this article I will
explain how you can use LINQ, when working with images in Web Application. For
best understanding we will work with the small project, named Image_WA. Ok,
let's start.

First of all we create ASP.Net Web Application project and add to it SQL Server
Database (fig. 1) :

Fig. 1.

Then we create a table for storing our images
(fig. 2):
Fig. 2.

As you can see, the table has seven columns (of corresponding Data Type), that
allow to store all information about our images. The column ForeingNum we use as
"connection" with another table (this is just foreign key for some table. Let's
assume, you store images of some rock groups; in this case all images of Deep
Purple have to have, for example, ForeingNum = 11; Pink Floyd, for example, - 12
and so on. Of course, you have to have some table RockGroups with the PrimaryKey
11, 12 etc.).

Now, we add to our project dbml file (LINQ to
SQL Class), named ImageDal. It will be our data access layer (Dal). All our
business logic we put into ImageBL class. Of course, we can create special BL
and Dal project, but our project is small enough, and there is no need to create
special projects. With the drag/drop operation we create Data Class Image (fig.
3):
Fig. 3.

To retrieve and then display our image dynamically we can use just aspx file or
the Handler class (ashx file). As usually, for binary file (this is: our images)
it's recommended to use the Handler class, but we will give example for both:
aspx and ashx files (by the way, about the Handler you can read: "What
is an ASHX file Handler or web handler" by Purushottam Rathore). With this
purpose we add to our project WebFormHandlerImage.aspx and ImageHandler.ashx
(again, in fact we have to use only one of them). Now our project looks so (fig.
4) and we are ready to add code to the pages and classes.

Fig. 4.

OK! Our ImageHandler has ProcessRequest method. We change two lines of the code
(which this method has by default) to the following:

To our ImageBL class we have to add at least methods to update data of some
image, insert some new data or delete, if necessary, already available data. We
have to have some method to get images with the same ForeignNum (see fig. 2).
All our adding will look so: