ASP.NET
  Home arrow ASP.NET arrow Page 5 - Using the SqlDataReader Class
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

Using the SqlDataReader Class
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-10-24

    Table of Contents:
  • Using the SqlDataReader Class
  • Retrieving data from the Pubs Database
  • Understanding the code example
  • More about the SqlDataReader Indexers
  • Using the SqlDataReader.GetValue() and GetOrdinal() methods
  • Returning Fields in appropriate data types

  • 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


    Using the SqlDataReader Class - Using the SqlDataReader.GetValue() and GetOrdinal() methods


    (Page 5 of 6 )

    In this example, we will use the SqlDataReader.GetValue() method to access the job_id and job_desc columns and return their values. We assign these values to the ListItem.Value and ListItem.Text properties respectively, and then add the newly created ListItem instance to the DropDownList control. You simply need to replace the above Page_Load() event handler method with the following code:

    protected void Page_Load(object sender, EventArgs e)

    {

      if (!IsPostBack)

    {

      try

    {

    using (SqlConnection connection = new SqlConnection
    (connectionString))

    {

    string commandText = "SELECT job_id, job_desc FROM Jobs";

      SqlCommand command = new SqlCommand(commandText, connection);

       connection.Open();

        using (SqlDataReader dataReader = command.ExecuteReader())

    {

      while (dataReader.Read())

    {

      ListItem item = new ListItem();

       item.Text = dataReader.GetValue(1).ToString();

       item.Value = dataReader.GetValue(0).ToString();

      DropDownList1.Items.Add(item);

       }

      }

     }

    }

      catch (Exception ex)

    {

      Response.Write(ex.Message);

     }

    }

    Label1.Text = "The list item " + DropDownList1.SelectedItem + " has value of " +

      DropDownList1.SelectedValue;

    }

    Note that we have used the ToString() method of the return value of the SqlDataReader.GetValue() method because it returns the value as an object data type.

    We can use the column name instead of column ordinal as in the following example. Just replace the the next Page_Load() event handler method with the one above and run the example.

     protected void Page_Load(object sender, EventArgs e)

    {

      if (!IsPostBack)

    {

      try

    {

    using (SqlConnection connection = new SqlConnection
    (connectionString))

    {

      string commandText = "SELECT job_id, job_desc FROM Jobs";

       SqlCommand command = new SqlCommand(commandText, connection);

        connection.Open();

         using (SqlDataReader dataReader = command.ExecuteReader())

    {

      while (dataReader.Read())

    {

      ListItem item = new ListItem();

    item.Text = dataReader.GetValue(dataReader.GetOrdinal
    ("job_desc")).ToString();

    item.Value = dataReader.GetValue(dataReader.GetOrdinal
    ("job_id")).ToString();

      DropDownList1.Items.Add(item);

       }

      }
     

     }

    }

      catch (Exception ex)

    {

      Response.Write(ex.Message);

     }

    }

    Label1.Text = "The list item " + DropDownList1.SelectedItem + "
    has value of " +

      DropDownList1.SelectedValue;

    }

    In the code above we accessed the columns by name but we did that using one more method call, in the same way as the column-name based indexer. We used the SqlDataReader.GetOrdinal() method, which accepts the column name as a string value, and returned the ordinal of that column; that's what is needed as an argument to the SqlDataReader.GetValue() method in order to return the value of the field. Let's see the Strongly Typed Get() methods that the SqlDataReader class provides.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
     

    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 6 hosted by Hostway
    Stay green...Green IT