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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 12
    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

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT