...

View Full Version : Just one table



hughesmi
12-22-2003, 08:35 PM
Hi. Can someone please help. I tried to modify the the table, in my asp script, please see this.

Response.Write("<table border=""1"" width=""486"" height=""106"">")

Response.Write("<tr>")

Response.Write("<td width=""160"" height=""35""><b>Name</b></td>")

Response.Write("<td width=""160"" height=""35""><b>Comments</b></td>")

Response.Write("<td width=""160"" height=""35""><b>Age</b></td>")

Response.Write("</tr>")

Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")

Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")

Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")

Response.Write ("</table>")

But I don't want it to write a new table each time it reads the database file. Can some try show me how you just update the data in the one table? I wil be happ to post the fulll script of needed

Roy Sinclair
12-22-2003, 10:59 PM
Response.Write("<table border=""1"" width=""486"" height=""106"">")
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""35""><b>Name</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Comments</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Age</b></td>")
Response.Write("</tr>")
do while not rsGuestbok.EOF
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")
Response.Write("</tr>")
rsGuestbook.ReadNext
loop

hughesmi
12-23-2003, 10:44 AM
Thanks for you help but I'm still have a few probs' can you please help me a bit fuhter? I have gave you all the asp script to see whats happing.

<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor="white" text="black">
<p><a href="guestbook_form.htm">Back to Form</a> </p>
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database



'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")

'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.Name, tblComments.Comments, tblComments.Age FROM tblComments;"

'Open the recordset with the SQL query
rsGuestbook.Open strSQL, adoCon

'Loop through the recordset
Do While not rsGuestbook.EOF

'Write the HTML to display the current record in the recordset
'Response.Write("<table border=""1"" width=""486"" height=""106"">")
'Response.Write("<tr>")
'Response.Write("<td width=""160"" height=""35""><b>Name</b></td>")
'Response.Write("<td width=""160"" height=""35""><b>Comments</b></td>")
'Response.Write("<td width=""160"" height=""35""><b>Age</b></td>")
'Response.Write("</tr>")
'Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")
'Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")
'Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")
'Response.Write ("</table>")

Response.Write("<table border=""1"" width=""486"" height=""106"">")
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""35""><b>Name</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Comments</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Age</b></td>")
Response.Write("</tr>")
do while not rsGuestbok.EOF
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")
Response.Write("</tr>")
Response.Write ("</table>")

'rsGuestbook.ReadNext
'loop


'Move to the next record in the recordset
rsGuestbook.MoveNext
Loop

'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>

Oakendin
12-23-2003, 05:27 PM
hughesmi, you didn't really describe your problem, but by looking at your code, are still have the same problem of opening your table and closing your table in a loop. Now, you've given yourself 2 loops. I can't recall trying a a loop in a loop with the same recordset before.

So I've modified your code. Its untested because I don't have your DB.



<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor="white" text="black">
<p><a href="guestbook_form.htm">Back to Form</a> </p>
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database



'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")

'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.Name, tblComments.Comments, tblComments.Age FROM tblComments;"

'Open the recordset with the SQL query
rsGuestbook.Open strSQL, adoCon

'Loop through the recordset, create a loop counter and set to zero
Dim loopCounter
loopCounter = 0
Do While not rsGuestbook.EOF

'If counter is less than one, but in the loop, start the table and header row
If loopCounter < 1 Then
Response.Write("<table border=""1"" width=""486"" height=""106"">")
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""35""><b>Name</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Comments</b></td>")
Response.Write("<td width=""160"" height=""35""><b>Age</b></td>")
Response.Write("</tr>")
End If

'The meat
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")
Response.Write("</tr>")

'Increment the counter
loopCounter = loopCounter + 1

'Move to the next record in the recordset
rsGuestbook.MoveNext
Loop
If loopCounter > 0 Then 'Logic is if there were records, the counter would have incremented, if greater than zero, close the table
Response.Write ("</table>")
End IF

'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>

A1ien51
12-23-2003, 06:08 PM
I personally would not response.write the entire table but on the variable. It makes the code look cleaner to the eye.

Just my 2 cents and not helping towards your question.....

Roy Sinclair
12-23-2003, 09:11 PM
Do it cleanly:



<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor="white" text="black">
<p><a href="guestbook_form.htm">Back to Form</a> </p>
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database



'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")

'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.Name, tblComments.Comments, tblComments.Age FROM tblComments;"

'Open the recordset with the SQL query
rsGuestbook.Open strSQL, adoCon
%>
<table border="1" width="486" height="106">
<tr>
<td width="160" height="35"><b>Name</b></td>
<td width="160" height="35"><b>Comments</b></td>
<td width="160" height="35"><b>Age</b></td>
</tr>
<%
'Loop through the recordset
Do While not rsGuestbook.EOF
Response.Write("<tr>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Name") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Comments") & "</td>")
Response.Write("<td width=""160"" height=""71"">" & rsGuestbook("Age") & "</td>")
Response.Write("</tr>")
'Move to the next record in the recordset
rsGuestbook.MoveNext
Loop

'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</table>
</body>
</html>

hughesmi
12-24-2003, 10:09 AM
thanks it worsk nice. I just needed a bit of help.

whammy
12-25-2003, 03:44 AM
Sorry for the late reply, but you didn't have a closing table row tag in your second table row (or an opening one for that matter!) - just to pinpoint the initial problem with your code since everyone fixed it, but didn't mention it specifically. :p

As is often the case, Roy Sinclair's code is a very good approach (although not the only way to do it), and may teach you a few things. I know I've learned a few things from him. :) Alien51 was pretty much getting at the same thing.

P.S. Sometimes you can benefit from looking at the generated HTML (i.e. "View Source" in a browser that is looking at the generated webpage) to quickly pinpoint problems like this, as well.

hughesmi
12-25-2003, 07:57 AM
Yes, thanks all for this I now have all the asp scripts working.

Just as one final point - do you have any good URLs to show me tips for fixing my little probs? I did look, but nothing that is outstanding.

hughesmi
12-29-2003, 06:58 PM
How would I make the table show just one recored. i,e like a confrimation page?

whammy
12-29-2003, 09:11 PM
You'd only pull that record in your SQL Statement. For instance let's say your primary key is called "ID", and you want to pull the record that has an ID of 50 from a querystring:

http://www.yourwebsite.com/something.asp?ID=50

would be your link... and then your code would look something like:



<%
Function ExtractNumbers(ByVal str)
If IsNull(str) Then Exit Function
Dim enRegEx
Set enRegEx = New RegExp
enRegEx.Pattern = "\D"
enRegEx.Global = True
ExtractNumbers = enRegEx.Replace(str,"")
Set enRegEx = Nothing
End Function

Dim ID, SQL, Conn, rs

ID = ExtractNumbers(Request.QueryString("ID"))

SQL = "SELECT * FROM TableName WHERE ID = " & ID

'Put your connection string here

Set rs = Conn.Execute(SQL)

If NOT rs.EOF Then
'Whatever you want to display here
Else
Response.Write("Nothing found!")
End If

Set rs = Nothing
Conn.Close
Set Conn = Nothing
%>


I'd definitely learn some SQL if you're going to be using databases, because it's a necessity. :)

P.S. That's the reason behind the "ExtractNumbers" function - you want to perform validation on any data you're going to be using in a SQL statement, otherwise your database could be compromised by an unscrupulous person. If you expect a whole number, you definitely want to make sure it _is one_ before using it in your WHERE clause!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum