Programming the ASP.NET 2.0 SqlDataSource Control - Using the SqlDataSource as our data access mechanism
(Page 2 of 4 )
In the previous section's code example, we used the SqlDataSource control to return and display data on an ASP.NET page, without using Data-Bound Controls such as the GridView control we used in the previous article. Let's walk through the code and discuss what we have done. We started by creating an object instance of our SqlDataSource control so we can work on its properties and methods in code. The next line of code adds the SqlDataSource1 object to the Controls collection of the page using the Add() method. You must use this technique to add controls programmatically in your code.
The SqlDataSource control is an abstraction of ADO.NET code. What do I mean by that? Okay, by using the SqlDataSource control you don't have to write ADO.NET code to connect to the database, execute a command, and then retrieve the result set using a DataReader object or a DataSet object. The SqlDataSource control accepts the connection string, to know where to get the data for you and the T-SQL Select statement that you need to execute on the server. It accepts the connection string and the T-SQL Select statement through its Properties as shown in the next 2 C# statements
SqlDataSource1.ConnectionString = "Data Source=(local);Initial
Catalog=Northwind;Integrated Security=True";
SqlDataSource1.SelectCommand = "SELECT EmployeeID, FirstName,
LastName, Title FROM Employees";
The SqlDataSource control's actual work, for selecting data operations, begins when you call its Select() method. Note that this method is usually called by the Data-Bound Controls when they need to display the data on an ASP.NET page and you don't have to explicitly call it, but we do that here to explain some important concepts regarding the SqlDataSource control. The SqlDataSource.Select() method returns an object that implements the IEnumerable interface. By implementing the IEnumerable interface, the contents of the Select() method's returned object type can be enumerated.
In plain English, when you call the SqlDataSource.Select() method it returns a collection of rows that you can iterate through in your code. Again, you wouldn't do that and it's the job of the Data-Bound Controls to iterate through the returned object of the Select() method, but it's good to understand how things work; it will be useful, as you will see. If you don't understand what the IEnumerable interface means to a collection of objects then I advice you to read more about C# Collections and the IEnumerable, IList, ICollection and IDictionary interfaces.
Anyway, the IEnumerable interface provides what the foreach statement needs to iterate over a collection. The returned object of the SqlDataSource.Select() method can be one of two possible objects. You can return a DataReader, which is a fast read-only forward-only mechanism for retrieving data, and you can return a DataSet, which is an in-memory representation of relational data. You can set what object is returned through assigning the SqlDataSource.DataSourceMode property to values of the SqlDataSourceMode enumeration. We will discuss this in the next section.
Next: Using the SqlDataSourceMode enumeration >>
More ASP.NET Articles
More By Michael Youssef