ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 - How did we obtain the rows?


    (Page 4 of 4 )

    To explain how we obtained the values of each of the returned rows I will remove the code that creates the HTML table's cells and rows from the above code. It looks like this:

     if (this.SqlDataSource1.DataSourceMode ==
    SqlDataSourceMode.DataReader)

    {

      foreach (DbDataRecord record in iteratorObject)

    {

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

    {

       string myField = record[i].ToString();

      }

     }

    }

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

     {

      foreach (DataRowView record in iteratorObject)

    {

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

    {

      string myField = record[i].ToString();

      }

     }

    }

    In the first foreach statement we get access to each row through an object of type DbDataRecord. The code we have written is flexible because we depend on the DbDataRecord.FieldCount property to tell us how many fields this row contains, and then using a for statement we access and retrieve the value of each field, through the indexer on the DbDataRecord, by passing the variable i to the indexer.

    In the second foreach statement we have different logic to use. The for statement uses the DataRowView.Row.Table.Columns.Count property. In short, this property contains the number of columns of the table that this DataView object represents, so now we know how many fields we should access through this property.

    Inside the for statement we have the same line of code as in the first foreach statement that accesses the value of the field through the indexer. This time the indexer works on another object which is a DataRowView object. Using this technique we can execute a T-SQL SELECT statement that selects any number of columns and still be able to retrieve them all.

    We can also access the fields by name but we have to modify the code. Note in this case we must know in advance how many fields are returned because we will access the fields by their names as shown in the next alternative code sample.

    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();

      HtmlTableCell cell = new HtmlTableCell();

       cell.Width = "60";

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

       row.Cells.Add(cell);

      cell = new HtmlTableCell();

      cell.Width = "60";

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

      row.Cells.Add(cell);

       cell = new HtmlTableCell();

       cell.Width = "60";

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

       row.Cells.Add(cell);
     
      cell = new HtmlTableCell();

      cell.Width = "60";

      cell.InnerHtml = "<b>" + record["Title"].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();

      HtmlTableCell cell = new HtmlTableCell();

       cell.Width = "60";

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

       row.Cells.Add(cell);

      cell = new HtmlTableCell();

      cell.Width = "60";

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

       row.Cells.Add(cell);

      cell = new HtmlTableCell();

      cell.Width = "60";

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

       row.Cells.Add(cell);

      cell = new HtmlTableCell();

      cell.Width = "60";

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

       row.Cells.Add(cell);


      table.Rows.Add(row);

     }

    }

      this.Controls.Add(table);

    }

    In the next article, we are going to perform update operations using the SqlDataSource control programmatically, and then in the next few articles we are going to use Data-Bound Controls to complete all these operations with "almost" no code.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    ASP.NET ARTICLES

    - 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
    - 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 ...





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