Professional Documents
Culture Documents
3.11 Practical 11
3.11 Practical 11
Practical Exercise 11
• Open the given [Practical11] ASP.NET web application project in Visual Studio.
• Create a new SQL Server Express database named [DemoDB.mdf] in the [App_Data] folder.
• Create new table named [Friend] with the following fields and add the following records
to the table:
[Id] is an identity
(auto increment) field
• The actual photos are stored in the [Photos] folder of the web application.
• Add a new DBML (LINQ to SQL Classes) file named [DemoDB.dbml] to the project.
• From Server Explorer, drag-and-drop the [Friend] table to the Object Relational (OR)
Designer. The following entity class should be generated:
• Drag-and-drop the given [SimpleImage.cs] file onto the project root in Solution Explorer.
• Open the [InsertFriend.aspx] page (which is partially completed). Currently, the page
can insert a [Friend] record. However, photo uploading has not been programmed.
• Select the RegularExpressionValidator that checks the file extension. In Properties Window,
set its ValidationExpression property to the following value:
• Press [F7] to switch to the associated C# file. Add the following using statement:
• In the btnInsert_Click event handler, locate the [TODO] section. Complete the section with
the following codes:
• Test the result. Ensure that input validation rejects none JPG and PNG file. Insert a new
[Friend] record. Ensure that the photo is uploaded, cropped, resized and saved successfully.
• Open the [EditFriend.aspx] page (which is partially completed). Currently, the page can
update and delete a [Friend] record. However, photo replacement and deletion have
not been programmed.
NOTE: While updating a [Friend] record, the user may or may not want to replace the photo.
Thus, if NO photo is selected in the FileUpload control, we assume that the user wants to
keep the existing photo. However, if a photo is selected, we assume that the user wants to
replace the existing photo with a new one.
• Select the RegularExpressionValidator that checks the file extension. In Properties Window,
set its ValidationExpression property to the following value:
• Press [F7] to switch to the associated C# file. Add the following using statements:
Namespace for
the File class
• In the btnUpdate_Click event handler, locate the [TODO] section. Complete the section with
the following codes:
NOTE: There are 2 steps in the photo replacement process: (1) Delete the existing photo,
and (2) Save the new photo (including cropping and resizing).
Since the new photo is having a different unique random filename. It is also necessary to
update the [Friend] record with the filename of the new photo.
• Test the result. Ensure that input validation rejects none JPG and PNG file. Update a [Friend]
record without selecting a photo. Then, update a [Friend] record with a new photo being
selected. Ensure that both cases are working as expected. Also ensure that the old photo has
been deleted from the [Photos] folder.
The record
should show the
new photo now
• In the btnDelete_Click event handler, locate the [TODO] section. Complete the section with
the following codes:
NOTE: Remember to delete the associated images (e.g. product images, profile images, etc)
while deleting the relevant database records (e.g. product records, member records, etc).
Leaving behind the images (which may no longer be used again) will be taking up the
storage space of the web server.
• Test the result. Delete a [Friend] record.Ensure that it is working as expected. Also ensure
that its photo has been deleted from the [Photos] folder.