Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 202,528 times

Contents

Related Categories

Beginning Active Server Pages - Getting DB Content

Getting DB Content

A more often task, however, is to retreive information from a database. This can be done using ADO recordsets and the Execute statement to run an SQL query.

First, using the form we created in the last section, add a few records to the database. In this section, you will find out how to display them! First, we execute an SQL statement, and assign a variable to its result:

Set rData = cConn.Execute ("My SQL Statement")

First, add the code below to a file called viewusers.asp. We've come across it all before.

<%
Dim cConn, rData
'Open the db connection
Set cConn = Server.CreateObject ("ADODB.Connection")
cConn.Open "test_db","",""
'Execute the SQL 
Set rData = cConn.Execute ("SELECT * FROM MyUsers")
%>

All the last statement does is return all the fields and rows from the table MyUsers into the recordset rData. rData is now a standard ADO Recordset.

We can use the rData.MoveNext command in a Do...Loop statement to loop through the data, and check the rData.EOF property to see if we have reached the end. All you need to do now is retreive the values for each field. This is done using rData("FieldName"). Using this information, we can now write some code to loop through the available records, and output some HTML:

<%
Dim cConn, rData
'Open the db connection
Set cConn = Server.CreateObject ("ADODB.Connection")
cConn.Open "test_db","",""
'Execute the SQL 
Set rData = cConn.Execute ("SELECT * FROM MyUsers")
%>

<p>Below is a list of the current members</p>
<table border="1">
 <tr>
  <td><b>ID</b></td>
  <td><b>Name</b></td>
  <td><b>Email</b></td>
 </tr>
<%
Do While rData.EOF = False
    'output ID
    Response.Write " <tr>" & vbNewLine & "  <td>" & rData("ID") & "</td>" & vbNewLine
    'output name
    Response.Write "  <td>" & rData("Name") & "</td>" & vbNewLine
    'output email
    Response.Write "  <td>" & rData("Email") & "</td>" & vbNewLine & " </tr>" & vbNewLine
    rData.MoveNext
Loop
%>

and that's it! It's really that simple... Now take a look in your browser to see the list of members who have signed up!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [1010] Beginning Active Server Pages - absolute beginner!

    Posted by michael poxon on 16 Dec 2006

        Hi,
    When I say I'm an ASP beginner, I mean it! I've only written one trivial bit of code, and got a blank page. I see now that's described in the article snippet below. I did ind...

  • .I want to know how to save the files.whats the extensions...

    Posted by writetoksk on 04 Oct 2006

    hi,


    its first i started ASP.I want to know how to save the files.whats the extensions...Where can i get the sample programs

  • Posted by James Crowley on 28 Dec 2004

    Are you running IIS ? And are you viewing it in your browser via the correct URL? (ie something starting with http:// rather than file:// ) ?

  • Posted by James Crowley on 28 Dec 2004

    It does - you just can't see it ;) We've got an ISAPI filter that rewritse /show/1010/ to something like /show.aspx?id=1010

  • It's not working ;_;

    Posted by HyperHacker on 12 Dec 2004

    Is there something special I have to do besides saving it as a .asp file, or does it just not work on my server? It just spits out the code, even HTML, as plain text.

    [code]
    Let's see if ASP...