JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Chapter 23: Database Access in Web Applications


Main Page

    Simple Data Binding
    Complex Data Binding
    Binding Data Grids
    Binding Standard Web Server Controls
    Navigating in Datasets
    Controls Designed for Use with Databases
    Using Data Grids in Web Applications
    Using Data Lists in Web Applications
    Using Repeaters in Web Applications
    Immediate Solutions: Using the BaseDataList Class
    Using the DataGrid Class
    Creating a Data Grid
    Binding Standard Controls
    Navigating in Datasets
    Using the DataList Class
    Creating a Data List
    Using the Repeater Class
    Creating Repeaters
    Using Data Readers
    Creating Master/Detail Web Forms
    Using XML-Format Databases Directly in the Internet Explorer

In Depth

In this chapter, we'll take a look at using databases in Web applications. The actual work you do with databases in Web applications is back on the server in Visual Basic code, of course, even if the database is on another Web server somewhere, so you still use the data connection, data adapter, and dataset objects that we've been using up to now. This means that the behind-the-scenes code that works with databases is going to be the same here as in previous chapters. We'll still use data adapters to fill datasets, and use connection objects to connect to databases, for example.

However, we're dealing with a different user interface here—Web forms. Web forms are displayed in the browser, which means your application doesn't have direct access to Visual Basic without being sent back to the server, and that's going to affect the way we do things here. The difference between working with data in Windows forms and Web forms is largely a matter of differences in data binding and in the types of controls you can use, as we'll see here.

Tip 

Needing a server roundtrip to access your data can slow things down considerably. The Internet Explorer actually does have a number of data source objects that you can use to work with recordsets directly with scripting languages in the browser. For a complete discussion on how to use those objects to support direct database handling in the Internet Explorer and how to work with that data in JavaScript, see the Coriolis HTML Black Book. One of the data source objects built into the Internet Explorer, the Remote Data Service (RDS), even lets you use connection strings, SQL, and so on, to fill a recordset object. For an example that uses the Internet Explorer XML data source object, which lets you read database files written in XML, see "Using XML-Format Databases Directly in the Internet Explorer" in the Immediate Solutions section of this chapter.

Because there's no direct connection maintained between a dataset and controls bound to that dataset, you're responsible for refreshing the data binding to controls in a Web form each time the page loads. In a Windows application, you don't need to do that; when the page loads, all you have to do is to clear the dataset and use a data adapter to fill a dataset, and the data bindings in the form are automatically updated:

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    DataSet11.Clear()
    OleDbDataAdapter1.Fill(DataSet11)
End Sub

In Web applications, the process is similar, but now you also have to explicitly run the DataBind method of any controls bound to the dataset, like this:

Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    DataSet11.Clear()
    OleDbDataAdapter1.Fill(DataSet11)
    TextBox1.DataBind()
End Sub

This refreshes the data in the bound control each time the page loads, which you don't have to do in Windows applications, because there, that connection is "live." Besides having to use DataBind to maintain data bindings, and aside from the obvious fact that Web server controls have fewer properties than Windows forms controls, working with databases in Web applications is remarkably similar to working with databases in Windows applications. Most of what we've done in the previous three chapters still applies here, because the actual work you do with a database is still done in Visual Basic code (this time on the server).

The biggest differences come when you're working with binding data to controls, but even here, the process is similar to what you'd do in Windows forms. For example, as with Windows database programming, there are two types of data binding—simple and complex.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor