03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

460 x CHAPTER 13 DISPLAYING AND UPDATING DATA<br />

How It Works<br />

You didn’t manually write any code to <strong>in</strong>teract with the database <strong>in</strong> this exercise, but you got a lot of<br />

functionality simply by dragg<strong>in</strong>g <strong>and</strong> dropp<strong>in</strong>g a database table. To see how it works, take a look at the<br />

source that VS generated. First, look at the markup for the SqlDataSource control:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

I changed the order of the attributes so all the comm<strong>and</strong>s are placed together. In your case, you may<br />

have the ProviderName attribute <strong>in</strong> a different location. As with all <strong>ASP</strong>.<strong>NET</strong> controls, the order of<br />

attributes <strong>in</strong> the control declaration doesn’t matter.<br />

A couple of <strong>in</strong>terest<strong>in</strong>g th<strong>in</strong>gs are worth exam<strong>in</strong><strong>in</strong>g. First, note that the ConnectionStr<strong>in</strong>g <strong>and</strong><br />

ProviderName attributes po<strong>in</strong>t to a connection str<strong>in</strong>g that has been def<strong>in</strong>ed <strong>in</strong> the Web.config file.<br />

You see more of this <strong>in</strong> the next section, <strong>in</strong>clud<strong>in</strong>g an explanation of the syntax used for the<br />

attributes.<br />

You then see four comm<strong>and</strong>s, each one of them conta<strong>in</strong><strong>in</strong>g a SQL statement that is used for one of<br />

the four operations of the CRUD acronym. The INSERT, UPDATE, <strong>and</strong> DELETE comm<strong>and</strong>s conta<strong>in</strong><br />

parameters, identified by the at symbol (@) prefix. At run time, when the control is asked to perform<br />

the relevant data operation, these parameters are substituted by runtime values. The SqlDataSource<br />

control keeps track of the relevant parameters <strong>in</strong> the *Parameters collections. For example, the<br />

element conta<strong>in</strong>s a s<strong>in</strong>gle parameter for the Id (the primary key) of the genre:<br />

<br />

<br />

<br />

Note that the Name of the parameter m<strong>in</strong>us the at symbol (@) l<strong>in</strong>es up with the parameter <strong>in</strong> the SQL<br />

statement:<br />

DeleteComm<strong>and</strong>="DELETE FROM [Genre] WHERE [Id] = @Id"

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

Saved successfully!

Ooh no, something went wrong!