ARTICLE

Creating Your First ADO.NET Web Application

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET May 02, 2010
In this article I will explain Creating Your First ADO.NET Web Application.
 
Reader Level:

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 Web Forms 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 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 reference to the System.Data.OleDb namespace to your project.

Listing 7-2. Filling data from a database to a ListBox control


Imports System
Imports System.Collections
Imports System.Configuration|
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.OleDb

Namespace firstADO
    Partial Public Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Put user code to initialize the page here
            ' Create a connection object
            Dim ConnectionString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & " Data source =c:/Northwind.mdb"
            Dim conn As New OleDbConnection(ConnectionString)

            ' open the connection
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If

            ' create a data adapter
            Dim da As New OleDbDataAdapter("Select customerID From customers", conn)

            'Create and fill a dataset
            Dim ds As New DataSet()
            da.Fill(ds)

            ' Bind dataset to the control
            ' Set DataSource property of ListBox as DataSet's DefaultView
            ListBox1.DataSource = ds
            ListBox1.SelectedIndex = 0

            ' Set Field Name You want to get data from
            ListBox1.DataTextField = "customerID"
            DataBind()

            ' Close the connection
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Sub
    End Class
End Namespace

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. At 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.jpg

Figure 7-17. Your first ADO.NET Web application

Conclusion


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.

Login to add your contents and source code to this article
share this article :
post comment
 

its more easier if you use the following command and its more benifitable than what we have seen here.



listbox1.items.add(dr(0))

this is a dynamic way and it doesnt need any other data sources.. so be simple and secure

Posted by praveenkumar sambu May 06, 2010
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor