Creating Your First ADO.NET Web Application

This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
I'll show you how to develop database applications using ADO.NET and ASP.NET. To start creating your first ADO.NET application, you'll create a Web Application project as you did in the previous section. In this example, you're adding only a List Box control to the page and you're going to read data from a database and display the data in the list box.
After dragging a list Box control from the WebForms control toolbox and dropping it on the page, write the code in Listing 7-2 on the Page_Load event. You can add a Page_Load event either by double-clicking on the page or using the Properties window.
Note: If you're using an access 2000 database and OleDb data providers, don't forget to add a reference to the System.Data.OleDb namespace to your project.
Listing 7-2. Filling data from a database to a ListBox control
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.OleDb;  
  15. namespace firstADO {  
  16.     public partial class _Default: System.Web.UI.Page {  
  17.         protected void Page_Load(object sender, EventArgs e) {  
  18.             // Put user code to initialize the page here  
  19.             // Create a connection object  
  20.             string ConnectionString = @ "Provider = Microsoft.Jet.OLEDB.4.0;" +  
  21.                 " Data source =c:/Northwind.mdb";  
  22.             OleDbConnection conn = new OleDbConnection(ConnectionString);  
  24.             // open the connection  
  25.             if (conn.State != ConnectionState.Open)  
  26.                 conn.Open();  
  28.             // create a data adapter  
  29.             OleDbDataAdapter da = new OleDbDataAdapter("Select customerID From customers", conn);  
  31.             //Create and fill a dataset  
  32.             DataSet ds = new DataSet();  
  33.             da.Fill(ds);  
  35.             // Bind dataset to the control  
  36.             // Set DataSource property of ListBox as DataSet's DefaultView  
  37.             ListBox1.DataSource = ds;  
  38.             ListBox1.SelectedIndex = 0;  
  40.             // Set Field Name You want to get data from  
  41.             ListBox1.DataTextField = "customerID";  
  42.             DataBind();  
  44.             // Close the connection  
  45.             if (conn.State == ConnectionState.Open)  
  46.                 conn.Close();  
  47.         }  
  48.     }  
As you can see, this code looks familiar. First, you create a connection object with the Northwind.mdb database. After that, you create a data adapter and select FirstName, LastName, and Title from the Employees table. Then you create a dataset object and fill it using the data adapter's Fill method. Once you have a dataset, you set the dataset as the ListBox's DataSource property and set SelectIndex as 0. The SelectIndex property represents the index of the column from a dataset you want to display in the control. The field name of your column is FirstName. In the end, you call the DataBind method of the ListBox. This method binds the data to the list box.
The output like figure 7-17. As you can see the ListBox control displays data from FirstName column of the Employees table.
Figure 7-17. Your first ADO.NET Web application


Hope this article would have helped you in understanding Creating Your First ADO.NET Web Application. See other articles on the website also for further reference.
adobook.jpg This essential guide to Microsoft's ADO.NET overviews C# then leads you toward a deeper understanding of ADO.NET.

Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.