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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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: 5 stars5 stars5 stars5 stars5 stars / 11
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - 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
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway