ADO.NET First Web Application in VB.NET

In this article I will explain Creating Your First ADO.NET Web Application.
  • 2158

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 readdata 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 ObjectByVal 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
            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()

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

            ' Close the connection 
            If conn.State = ConnectionState.Open Then
            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. 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.


More Articles

© 2020 DotNetHeaven. All rights reserved.