AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=174&pId=-1
Basic Databases Part 2
page
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24581/ 45

Introduction

Basic Database Publishing, Part 2 / Data to Table

Introduction

Now that you've done part one, its time to continue on with the organization of the data into a table. (This article is also named 'Data to Table'). If you are reading from Part 1 then all you need is the recordset and the connection. If you are reading from Data to Table then Step 4 really = Step 1 for you, but be sure that you have your recordset and your connection done.

Step 4 : Basic Tabling

Step 4: Basic Tabling

I'm going to create a table as you'll see in this next one, with 1 column and then populate it with data from Addresses (This code is outside ASP tags so be sure to close them off).

<table border=1><tr><td><strong>Addresses</strong></td></tr>
<% While Not RecordSet1.EOF
Response.Write("<tr><td>")
%><%=(Recordset1.Fields.Item("Address").Value)%><%
Response.Write("</td></tr>")
RecordSet1.MoveNext
Wend %>
</table>

See! Simple, we create the table parameters and then whenever there is a record we stick a new row in and shove the address in, close the row and move on to the next one.

Step 5 : More, more, MORE!

Step 5 : More, more, MORE!

So you want more than 1 column eh? Well simply add a bit more code to the While statement.

<table border=1><tr><td><strong>Addresses</strong></td><td><strong>City</strong></tr>
<% While Not RecordSet1.EOF
Response.Write("<tr><td>")
%><%=(Recordset1.Fields.Item("Address").Value)%><%
Response.Write("</td><td>") 'Don't Finish the row yet
%><%=(Recordset1.Fields.Item("City").Value)%><% 'Put in a column with some 'more data
RecordSet1.MoveNext

Wend %>
</td></tr></table>

If you know your HTML, you see that we didn't end the row quite yet, we added another column and placed the city value there, simple, and it looks like this:

Step 6 : Alternating Colour

Step 6 : Alternating color

Ok, that may be fine for you, but I like to alternate my color. This requires a bit more work. We have to put 2 rows of data in 1 While statement and do the row color at the same time.

<table border=1><tr><td><strong>Addresses</strong></td><td><strong>City</strong></tr>
<% While Not RecordSet1.EOF
Response.Write("<tr bgcolor=gray><td>") 'Put in a bgcolor for the 1st row
%><%=(Recordset1.Fields.Item("Address").Value)%><%
Response.Write("</td><td>")
%><%=(Recordset1.Fields.Item("City").Value)%><%
RecordSet1.MoveNext
'From here down, copy and paste from above.
Response.Write("<tr bgcolor=silver><td>") 'Change bgcolor for the 2nd row
%><%=(Recordset1.Fields.Item("Address").Value)%><%
Response.Write("</td><td>")
%><%=(Recordset1.Fields.Item("City").Value)%><%
RecordSet1.MoveNext
Wend %></td></tr></table>

Simple copy and paste from the comment but I change the row bgcolor on the second one. And this looks like this:

Summary

Wrap up

We're done! Now you know how to connect to a database, and display it to a table with alternating colors! If you are looking forward to ASP.Net you should know that you can replace the whole While statement with 1 line of code and add much more functionality.

For more info on this stuff, watch out for my next tutorial on adding more columns with custom pictures, making data into links and having it all done in ASP.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 6:42:25 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search