ASP.NET
  Home arrow ASP.NET arrow Page 2 - 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 - Retrieving data from the Pubs Database


    (Page 2 of 6 )

    In this example, we are going to use the SqlDataReader class to retrieve data from the Jobs table in the Pubs database. We will use a DropDownList control to store the retrieved data. Start by creating a new website and add a DropDownList control along with a Label control to the form. The markup for the form would look like this:

    <form id="form1" runat="server">

      <div>

    <asp:DropDownList ID="DropDownList1" runat="server" Width="200px"
    AutoPostBack="True">

      </asp:DropDownList><br /><br />

    <asp:Label ID="Label1" runat="server"
    ForeColor="Red"></asp:Label>

      </div>

    </form>

    Note that the AutoPostBack attribute, or you would say property if you are adding it in C# code, is set to true, which posts the page back to the server when you change the selected item. We need this behavior to display the selected item and its value in the Label control every time the user selects another item from the DropDownList control. In the Default.aspx.cs file add a namespace reference statement for the SQL Server .NET Data Provider as in the following line of code:

    using System.Data.SqlClient;

    Now add a private instance variable to hold the Connection String as follows:

    private string connectionString = "Data Source=(local);Initial
    Catalog=Pubs;Integrated Security=True";

    The code that performs the data access and populates the DropDownList control is written in the Page_Load event handler method as follows:

     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(dataReader[1].ToString(), dataReader
    [0].ToString());

    // or you can replace the above line of code with the following
    one

    //ListItem item = new ListItem(dataReader["job_desc"].ToString(),

    // dataReader["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;

    }

    Let's explain the code.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
     

    ASP.NET ARTICLES

    - 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
    - Developing a Dice Game Using ASP.NET Futures...





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