As usual, we will use a very simple aspx page containing only a button to request retrieval of the data. The actual work will be in the code-behind file. The aspx file is shown below without much explanation except to point out that it contains a button to call a routine to display the data.
|
<%@ Page Language="vb" Src="ProcessData.aspx.vb" Inherits="ProcessData"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>ProcessData</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body> <form runat="server"> <asp:Button id="btnGetData" runat="server" text="Get Data" onclick="Get_Data" /> </form> </body> </html> |
|
Imports System Imports System.Web.UI Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class ProcessData : Inherits Page Sub Get_Data(Sender As Object, e As EventArgs) Dim intColIndex As Integer Dim objConn As New SqlConnection( _ ConfigurationSettings.AppSettings("ConnectionString")) Dim cmdCustomers As New SqlCommand("Select Top 10 CustomerID, " _ & "CompanyName, ContactName, " _ & "ContactTitle, Phone, Fax From Customers", objConn) Dim dataReader As SqlDataReader objConn.Open() dataReader = cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection) Response.Write("<table border='1'>") While dataReader.Read Response.Write("<tr>") For intColIndex = 0 To dataReader.FieldCount - 1 Response.Write("<td>") Response.Write(dataReader.GetValue(intColIndex)) Response.Write("</td>") Next Response.Write("</tr>") End While Response.Write("</table>") dataReader.Close() End Sub End Class |