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.

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

Data Source Controls<br />

The Data category of the Toolbox conta<strong>in</strong>s six different data source controls that you can use to<br />

b<strong>in</strong>d data to your data-bound controls. The XmlDataSource <strong>and</strong> SiteMapDataSource controls are<br />

used to b<strong>in</strong>d hierarchical, XML-based data to these controls. You saw SiteMapDataSource at work<br />

when you created the site map <strong>in</strong> Chapter 7.<br />

The ObjectDataSource control enables you to connect your data-bound controls to separate objects<br />

<strong>in</strong> your application. Instead of ty<strong>in</strong>g your data-aware controls directly to a database, you b<strong>in</strong>d data<br />

from a separate layer with custom objects to them. Get yourself a copy of Wrox’s Professional<br />

<strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> (ISBN 978-1-118-31182-0) if you want to f<strong>in</strong>d out more about the ObjectDataSource<br />

control.<br />

The f<strong>in</strong>al three data source controls are the SqlDataSource, the EntityDataSource, <strong>and</strong> the<br />

L<strong>in</strong>qDataSource controls. The first two are discussed <strong>in</strong> this chapter <strong>and</strong> Chapter 14, respectively.<br />

The L<strong>in</strong>qDataSource serves as a data source for LINQ to SQL, a technology similar to the ADO.<br />

<strong>NET</strong> Entity Framework you learn more about <strong>in</strong> Chapter 14. Because Microsoft is now promot<strong>in</strong>g the<br />

Entity Framework <strong>in</strong>stead of LINQ to SQL, I won’t discuss the L<strong>in</strong>qDataSource control <strong>in</strong> this book.<br />

The QueryExtender control acts like an add-on to the L<strong>in</strong>qDataSource <strong>and</strong> EntityDataSource<br />

controls <strong>in</strong> that it enables you to create a rich filter<strong>in</strong>g <strong>in</strong>terface to search for specific data without<br />

manually writ<strong>in</strong>g a lot of code. You can learn more about the QueryExtender <strong>in</strong> this article:<br />

http://bit.ly/92kMPQ.<br />

Other Data Controls<br />

The f<strong>in</strong>al control <strong>in</strong> the Toolbox is the Chart control. It was <strong>in</strong>itially released as an add-on to Visual<br />

Studio 2008 but has now been fully <strong>in</strong>tegrated <strong>in</strong>to VS 2012. It’s designed to render chart graphics<br />

rang<strong>in</strong>g from simple bar charts to 3-D pie charts <strong>and</strong> fancy l<strong>in</strong>e diagrams. I won’t discuss this control<br />

any further, but you can f<strong>in</strong>d a series of articles that discuss it <strong>in</strong> detail here: http://t<strong>in</strong>yurl<br />

.com/nsnbvv.<br />

In the next section you see how to use the SqlDataSource <strong>and</strong> the GridView to retrieve <strong>and</strong> display<br />

data from a database. Later sections <strong>and</strong> chapters dig deeper <strong>in</strong>to the other data controls.<br />

DATA SOURCE AND DATA-BOUND CONTROLS<br />

WORKING TOGETHER<br />

The SqlDataSource control enables you to quickly create functional, database-driven web pages.<br />

Without writ<strong>in</strong>g a whole lot of code, you can create web pages that enable you to perform all four<br />

operations of the CRUD acronym: Create, Read, Update, <strong>and</strong> Delete data. Although its name may<br />

seem to imply that the control can access only Microsoft’s SQL Server, that’s not the case. The control<br />

can access other databases, such as Oracle or MySQL, as well.<br />

Display<strong>in</strong>g <strong>and</strong> Edit<strong>in</strong>g Data with GridView<br />

To give you an idea of how the SqlDataSource control works <strong>in</strong> conjunction with the data-bound<br />

controls, the next Try It Out shows you how to create a very simple data-driven web page that

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

Saved successfully!

Ooh no, something went wrong!