ASP.NET
  Home arrow ASP.NET arrow Page 2 - Programming the ASP.NET 2.0 SqlDataSource ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Programming the ASP.NET 2.0 SqlDataSource Control
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2007-10-30

    Table of Contents:
  • Programming the ASP.NET 2.0 SqlDataSource Control
  • Using the SqlDataSource as our data access mechanism
  • Using the SqlDataSourceMode enumeration
  • How did we obtain the rows?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Michael Youssef


       · In my article 'Introduction to ASP.NET 2.0 SqlDataSource Control' we have seen how...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek