ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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 
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 - Using the SqlDataSourceMode enumeration


    (Page 3 of 4 )

    Using the SqlDataSourceMode enumeration makes a huge difference for us. If your SqlDataSource control is used to retrieve data for display purposes only, you should set the SqlDataSource.DataSourceMode property to SqlDataSourceMode.DataReader. If you need to retrieve data and perform operations such as updating, deleting, sorting and filtering then you would set the SqlDataSource.DataSourceMode property to SqlDataSourceMode.DataSet.

    Take a look at our code again.

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

     }

    }

    The if statement evaluates to true if the SqlDataSource1.DataSourceMode is equal to SqlDataSourceMode.DataReader. If it is, we iterate through the returned Data Reader object's collection of DbDataRecord objects. The returned DbDataRecord objects represents the table's rows that are returned using a Data Reader object. The else if statement evaluates to true if the SqlDataSource1.DataSourceMode is equal to SqlDataSourceMode.DataSet, which executes a different code because the returned object, in this case, is a DataView object and its collection of the database table's rows are not the same as the DataReader object's collection of database table's rows.

    The DataView object has a collection of DataRowView that represents the returned rows. Note that both the DataReader and the DataView implement the IEnumerable interface and that's why we can enumerate through its collection of rows. Also note that the DataReader returned, if the SqlDataSourceMode.DataReader is used, depends on the ADO.NET Data Provider used. By default the SQL Server Data Provider is used and this means that a SqlDataReader object is the return type of the SqlDataSource.Select() method.

    If we don't test for the value of the SqlDataSource.DataSourceMode property we would get an exception, shown in the next screenshot. If the SqlDataSource.DataSourceMode is set to SqlDataSourceMode.DataReader our code implements logic to manipulate a returned DataView object as shown next.

    SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;

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

    }

    The exception is shown in the following screenshot.

    And the same InvalidCastException is raised in the opposite situation, where you will have code that manipulates the DataReader object but the returned object is DataView as shown next.

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

    }

    The default value for the SqlDataSource.DataSourceMode is DataSet so if you tried to execute the above code without the if statement you would get the exception shown in the next screenshot.

    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

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





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