17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: <strong>FILESTREAM</strong> with ASP.NET and SilverlightWriting the "code-behind"<strong>The</strong> next task is to write the code that runs behind the web page. When the user clicksthe Save button, we need to save the item information, including the image file, into thedatabase. In this section, we will see the code required to implement this functionality.We will start with the import statements. Add the import statements in Listing 5-5 to thecode file associated with your web page.using System.IO;using System.Data.SqlClient;using System.data;Listing 5-5:Import statements.To make our code run when the user clicks the Save button, we'll add a handler for theClick event on the button. In a real-world scenario, you might want to do a number <strong>of</strong>validations before saving the information into the database, such as checking whetherthe user has entered all the mandatory fields. To keep the code simple in this lab, we willskip these validations and validate only the file upload control; we will proceed with thedatabase call only if the user has selected a file using the file upload control provided onthe web page. This can be verified by checking the HasFile property <strong>of</strong> the file uploadcontrol, as shown in Listing 5-6.protected void btnSave_Click(object sender, EventArgs e){if (fpItemImage.HasFile){SaveItem();}}Listing 5-6:Handler for Click event <strong>of</strong> the button.171

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!