Displaying Data on a WebForm

Team Fly 

Page 273

The client's browser accepts this composed HTML, of course (though it would likely reject an executable object, such as a traditional ActiveX control, for security reasons). In this way, a relatively sophisticated user-interface is created in the client browser—sophisticated compared to traditional HTML GUI controls such as a Submit button. When the user interacts with their browser, the results are posted back to the server, which can then send a response to the client. In this way, rich client controls are made available, in spite of the usual security and bandwidth problems. The server control solution is effective for most kinds of GUI. Server controls are also browser agnostic, so the few remaining users of Netscape products can see the GUI too.

If you have any doubts about the efficacy of server controls, compare the more flexible, sophisticated visual and user-interaction features of the DataGrid (discussed later in this chapter) with the rather poor features of the traditional HTML table. If you want to display data in browsers, the DataGrid is clearly the superior choice.

Displaying Data on a WebForm

For the first example in this chapter, let's see how to use the code-behind feature of the .NET WebForm to make a connection to a database, then display some of its fields on the client browser. This example involves no server-side controls; it's simply directly written on the user's browser with the HTML Response.Write command.

Start a new VB.NET ASP.NET Web Application. Double-click the WebForm to get to the code-behind window (by default it's named WebForm1.aspx.vb).

Add these two Imports statements to the top of the code window:

 Imports System.Data Imports System.Data.SqlClient Then type Listing 11.1 into the Page_Load event. 

LISTING 11.1: CONNECTING TO A DATABASE

Private Sub Page_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load

        Dim conString As SqlConnection = New SqlConnection _
(''Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs")
        conString.Open()

        Dim SQLc As SqlCommand = New SqlCommand("SELECT * FROM Authors", conString)
        Dim datReader As SqlDataReader = _
SQLc.ExecuteReader(CommandBehavior.CloseConnection)

        While datReader.Read
            Response.Write(datReader.GetString(1) & ",")
            Response.Write(" " & datReader.GetString(2))
            Response.Write(" --- " & datReader.GetString(3)) 

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net