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

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT