Creating an ADO.NET connection


To retrieve data from your database, you'll need ADO.NET. If you're familiar with IIS and the Web folder hierarchy, you probably don't need any help setting up an example. You can follow ours by copying Northwind (the sample database that comes with Access) to the Inetpub\wwwroot folder on your local system. Our example is in a Web folder named nettest. Cut and paste (or enter) the code example into a text editor and save it as nettest.aspx.

Now, import the .NET namespace that allows you to work with OLEDB databases:
<%@ Import Namespace="System.Data.OleDb" %>

The PageLoad event executes the code that connects to the Northwind database, and Server.mappath returns the physical path to the folder that contains it, as shown in Listing A.

Connection strings
If you wanted to connect to SQL Server using an OLEDB connection, you could use the following:
"Provider=sqloledb;Data Source=Martin;Initial Catalog=NorthWind;Integrated Security=SSPI;"

If you're working with Oracle, you could use:
"Provider=msdaora;Data Source=OracleDataBase;User Id=YourUserName;Password=YourPassword;"

A useful resource for connection information is connectionstrings.com, which contains a connection string for every situation imaginable.

Creating the ADO.NET objects
The next step is to create a DataReader object to hold the data you want to display. The following code uses the Command object's ExecuteReader method to create a DataReader object that will store all the records from the Northwind Customers table:
cnn.Open()
sql="SELECT CompanyName, ContactName, Address, City FROM Customers"
cnn=New OleDbCommand(sql,cnn)
dbread=cnn.ExecuteReader()


This code opens the connection and defines the data-retrieving SQL statement. Then the ExecuteReader method creates the DataReader object (dbread). Note that OleDbCommand passes both the connection and the SQL statement. The DataReader control then returns a stream of read-only data to the client.

Using a Repeater control to display the data
Now you can use a Repeater control to display the data by binding the DataReader object. The Repeater control lets you construct a simple template (for example, an HTML table) that's repeated for each row of data returned by the query.

Use a HeaderTemplate block to create the initial table structure; the data will appear within the ItemTemplate (table rows and columns) block. For example, the code below creates a table header for our customer data that refers to the fields returned by the earlier SQL statement:
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>CompanyName</th>
<th>ContactName</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>


Unlike other ASP.NET objects, the Repeater has no layout or styles available; you must define your own. Each row returned is displayed by the ItemTemplate block. The following script contains one cell (in the HTML table) for each field:
<ItemTemplate>
<tr>
<td><%#Container.DataItem("CompaName")%></td>
<td><%#Container.DataItem("ContactName")%></td>
<td><%#Container.DataItem("Address")%></td>
<td><%#Container.DataItem("City")%></td>
</tr>
</ItemTemplate>


You can't tell from the above example, but the template code is within the HTML body tags but outside the script definition.

Viewing the .NET page
The code in Listing B displays the customer data within the browser using an HTML table template to display the items.

Cut and paste this script into a text editor and be sure to save it with the .aspx extension. Then save or move the .aspx file to the Web root folder (wwwroot\nettest, for this example). Launch your browser and enter the appropriate address to open this file. When viewed in the browser (Figure A), the file displays a simple HTML Web page and the requested data, as defined by the SQL statement query.

You can also improve the look of the page. For example, you can alternate row colors by adding another template section. Specifically, the AlternatingItemTemplate block changes the background color of each table cell. The following script changes the cell background to yellow (FFFF00):
<AlternatingItemTemplate>
<tr bgcolor="#FFFF00">
<td><%#Container.DataItem("companyname")%></td>
<td><%#Container.DataItem("contactname")%></td>
<td><%#Container.DataItem("address")%></td>
<td><%#Container.DataItem("city")%></td>
<td><%#Container.DataItem("Region")%></td>
</tr>
</AlternatingItemTemplate>


To affect every other row, place the AlternatingItemTemplate block after the ItemTemplate block.

Conclusion
.NET isn't exactly new on the scene. But if you have delayed making the leap from classic ASP to ASP.NET, now's the time to get started. If you have some solid experience with ASP and ADO, you should be able to make the transition fairly easily.

Related links

Comments

1

jdave - 16/01/07

this page help me and give me idea...can you please tell me and give me a source code to creating a comment-post page, where our visitors can post a comment and view their email add? thanks

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

jdave - 16/01/07

this page help me and give me idea...can you please tell me and give me a source code to creating a ... more

Log in


Sign up | Forgot your password?

Most popular tags

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!