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

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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek