ASP.NET
  Home arrow ASP.NET arrow 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


    (Page 1 of 4 )

    Many of us think that Data-Source controls, like the SqlDataSource control, have been developed to work with Data-Bound controls. Yes this is correct and this is exactly the way we should be using it. But in this article, I'm going to show you that you can use the SqlDataSource control without an associated Data-Bound control like the GridView control.

    Today we are going to write C# code in the Page_Load() event handler method of the Default.aspx page. It creates a SqlDataSource object and assigns appropriate values for the control's required properties. These properties are used by the SqlDataSource control to communicate with the database, and then call the appropriate methods. That will help you to understand the difference between the code that we write today and the code that was generated by Visual Studio.NET 2005 in the last article, "Introduction to the ASP.NET 2.0 SqlDataSource Control." In that article, we used the SqlDataSource control's Task Menu to make the configuration for the control. The code we write today will help you understand how the SqlDataSource control works that way, so let's start.

    Create a new web site, then add a new Web Form to it and write the following code in place of the code of the Default.aspx.cs file. Note: don't add any controls to the markup.

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    using System.Collections;
    using System.Data.Common;

    public partial class _Default : System.Web.UI.Page

    {

      protected void Page_Load(object sender, EventArgs e)

    {

      SqlDataSource SqlDataSource1 = new SqlDataSource();

       this.Controls.Add(SqlDataSource1);

    SqlDataSource1.ConnectionString = "Data Source=(local);Initial
    Catalog=Northwind;Integrated Security=True";

    SqlDataSource1.SelectCommand = "SELECT EmployeeID, FirstName,
    LastName, Title FROM Employees";

    IEnumerable iteratorObject = SqlDataSource1.Select
    (DataSourceSelectArguments.Empty);

      HtmlTable table = new HtmlTable();

    if (SqlDataSource1.DataSourceMode ==
    SqlDataSourceMode.DataReader)

    {

      foreach (DbDataRecord record in iteratorObject)

    {

      HtmlTableRow row = new HtmlTableRow();

       for (int i = 0; i < record.FieldCount; i++)

    {

      HtmlTableCell cell = new HtmlTableCell();

        cell.Width = "60";

         cell.InnerHtml = "<b>" + record[i].ToString() + "</b>";

         row.Cells.Add(cell);

    }

      table.Rows.Add(row);

     }

    }

    else if (SqlDataSource1.DataSourceMode ==
    SqlDataSourceMode.DataSet)

    {

      foreach (DataRowView record in iteratorObject)

    {

      HtmlTableRow row = new HtmlTableRow();

       for (int i = 0; i < record.Row.Table.Columns.Count; i++)

    {

      HtmlTableCell cell = new HtmlTableCell();

       cell.Width = "60";

       cell.InnerHtml = "<b>" + record[i].ToString() + "</b>";

        row.Cells.Add(cell);

    }

      table.Rows.Add(row);

     }

    }

      this.Controls.Add(table);

     }

    }
     

    Run the page and you will get the result shown in the following screenshot.

    As you can see, we have accessed the database and retrieved data without writing any ADO.NET data access code. Let's see how we have accomplished this result.

    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

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT