ASP.NET
  Home arrow ASP.NET arrow Page 2 - Introducing ADO.NET with ASP.NET 2.0
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

Introducing ADO.NET with ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-09-19

    Table of Contents:
  • Introducing ADO.NET with ASP.NET 2.0
  • Populating a ListBox control from the Northwind database
  • Explaining the code
  • Using Exception Handling code

  • 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


    Introducing ADO.NET with ASP.NET 2.0 - Populating a ListBox control from the Northwind database


    (Page 2 of 4 )

    For this example we will populate a ListBox control with data from the Employees table of the Northwind database. We will select only the FirstName and the LastName columns from the Employees table, and add the rows as list items to the ListBox control. Let's do it. Start by creating a new website: File --> New --> Website, and place the website folder on the file system. Drag a ListBox control on the page and set its width to 157 and its height to 143 (pixels). That's all that you need in the Default.aspx file. 

    The following is the code of the Default.aspx.cs file (you need to replace the auto-generated code template with the following code or write it by hand step by step):

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    // use the namespace of the ADO.NET SQL Server Data Provider
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    // create the connection and set the connection string
    // through the connection object's ConnectionString property
    SqlConnection connection = new SqlConnection();
    string connectionString = "Data Source=(local);Initial
    Catalog=Northwind;Integrated Security=True";
    connection.ConnectionString = connectionString;

    // create the command object and assign the connection object to
    it
    // through the Connection property
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    // creating the T-SQL SELECT statement and assign it to the
    command
    // through the CommandText property
    string commandText = "SELECT LastName, FirstName FROM
    Employees";
    command.CommandText = commandText;
    // assigning the value CommandType.Text to the CommandType
    property
    // because we are using a T-SQL statement not a stored procedure
    command.CommandType = CommandType.Text;

    // we have initialized the connection and the command so it's
    time
    // to open the connection and execute the command
    connection.Open();
    // get a SqlDataReader object from the Command's ExecuteReader()
    method
    SqlDataReader dataReader = command.ExecuteReader();

    // adding the returned rows to the listbox control
    while(dataReader.Read())
    {
    ListBox1.Items.Add(dataReader[0] + ", " + dataReader[1]);
    }
    dataReader.Close();
    connection.Close();
    }
    }
     

    Now run the page and you will get the first names and last names of the employees added to the list box control as shown in the following screenshot.

    More ASP.NET Articles
    More By Michael Youssef


       · Yes you can use the SqlDataSource control without knowing much about ADO.NET 2.0,...
     

    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





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