08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

268<br />

CHAPTER 11 ■ <strong>AJAX</strong> APPLICATIONS AND EMPOWERING THE WEB USER EXPERIENCE<br />

This simply checks to see if the parameter is present. If it is, it loads its value into the<br />

integer picID; otherwise it defaults it to 100.<br />

Next, you set up your data connection <strong>and</strong> initialize a SQL query to use this<br />

parameter:<br />

string connectionString =<br />

<strong>Web</strong>ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"]<br />

.ConnectionString;<br />

SqlConnection con = new SqlConnection(connectionString);<br />

string sql = "Select * from Production.ProductPhoto where ProductPhotoID=@ID";<br />

SqlComm<strong>and</strong> cmd = new SqlComm<strong>and</strong>(sql, con);<br />

cmd.Parameters.Add("@ID", picID);<br />

This code pulls the connection string from the <strong>Web</strong>.Config file for the web application.<br />

If you don’t have one already, refer back to Chapter 4 for instructions on installing<br />

<strong>and</strong> configuring the database on your web site.<br />

It then creates a new instance of a SqlConnection object, initializing it with this connection<br />

string.<br />

The SQL is simply a string, <strong>and</strong> in SQL strings you can specify parameters using the<br />

@ directive, so it knows that when you say ProductPhotoID=@ID, it will be expecting a<br />

parameter.<br />

Next, a new SqlComm<strong>and</strong> is created using this SQL code on the configured connection.<br />

Finally, the parameter is added to the comm<strong>and</strong>. SQLComm<strong>and</strong> objects have a collection of<br />

parameters that is empty by default. When you add parameters to this collection, you<br />

specify the parameter name (@ID in this case) <strong>and</strong> the parameter value (picID in this case).<br />

When the comm<strong>and</strong> is executed, the SQL is assembled with the parameter values filling<br />

in the placeholders.<br />

The next step is to open the database <strong>and</strong> execute the SQL. When you execute a comm<strong>and</strong>,<br />

a SqlDataReader object is returned, allowing you to read the results line by line.<br />

This query will at most return one line (as there is only one picture per ProductPhotoId).<br />

con.Open();<br />

SqlDataReader sqlRead = cmd.ExecuteReader();<br />

To access the contents of the SqlDataReader, the Read function is called. This reads a<br />

single record at a time. As we have at most one record, we call this once. Calling this function<br />

loads the next record, <strong>and</strong> returns true if it succeeds (i.e., if there is a next record),<br />

<strong>and</strong> false if it doesn’t.<br />

Thus, in this case, we can gate the sqlRead.Read, <strong>and</strong> in the true clause h<strong>and</strong>le the<br />

loading <strong>and</strong> writing of the image to the response. In the false clause, we can provide an<br />

error message. Here’s the code:

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

Saved successfully!

Ooh no, something went wrong!