Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With Simple ASP Query 1

Status
Not open for further replies.

phzero

Programmer
Joined
Feb 14, 2002
Messages
86
Location
ZA
Hi all, below please find my source code for my ASP page. What I'm trying to do is to populate the dropdown box called "ProbCat" in the form. All the code works. If I comment out the document... line and put it in a MsgBox, it displays the names from the database. Could anybody please tell me what I'm doing wrong. Any comments would be appreciated.


<HTML>
<HEAD>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;>
<meta http-equiv=&quot;cache-control&quot; content=&quot;private, no-cache, must-revalidate&quot;>
<link rel=&quot;stylesheet&quot; href=&quot;styles/global.css&quot; type=&quot;text/css&quot;>

<script language=&quot;vbscript&quot;>
sub GetData()
dim db, rs
set db = CreateObject(&quot;adodb.connection&quot;)
set rs = CreateObject(&quot;adodb.recordset&quot;)
db.Provider = &quot;sqloledb&quot;
db.ConnectionString = &quot;driver={SQL Server};server=127.0.0.1;uid=gino;pwd=global;database=MyWebDatabase;&quot;
db.Open
set rs = db.Execute(&quot;select * from GuestInfo&quot;)

rs.MoveFirst
while not rs.EOF
document.LogIncident.ProbCat(&quot;mother&quot;).add rs(&quot;name&quot;)
rs.MoveNext
wend
rs.Close
db.Close
end sub
</script>

</HEAD>
<BODY>
<form name=&quot;LogIncident&quot; method=&quot;post&quot; action=&quot;LogIncident.asp&quot;>
<table>
<tbody>
<tr><td colspan=2><h1>Please Log Your Request</h1></td></tr>
<tr><td colSpan=2><h4>Incident Category:</h4></td></tr>
<tr>
<td class=&quot;label&quot;>Problem Category:</td>
<td>
<select name=&quot;ProbCat&quot;>
<option name=&quot;mother&quot;>
</select>
</td>
</tr>
<tr><td>&nbsp;</td></tr>
</tbody>
</table>
<input class=&quot;button&quot; value=&quot;Click me&quot; onclick=&quot;GetData()&quot;>
</form>
</BODY>
</HTML>
 

you are trying to populate the data when the user clicks the button

populate it before you serve the page

<select name=&quot;ProbCat&quot;>
<% GetData %>
</select>


and in the procedure:

while not rs.EOF
response.write &quot;<Option value=&quot; & rs(&quot;name&quot;) & &quot;>&quot; & rs(&quot;name&quot;) & &quot;</option>&quot;
rs.MoveNext
wend

 
The first problem I notice with your script is that it is all being performed client-side. Is that really what you want (the user will be able to view all your connection information, including username/password)?!

Also, I don't quite understand the point of only filling in the select list when the user clicks the button?

So, firstly, put your ASP code between ASP delimiters <% %>. Then get rid of the sub and just output the select list when you load the page:

Code:
<%
'open connection

strSQL = &quot;SELECT col1 FROM table1&quot;

Set rs = objConn.Execute(strSQL)
%>

...
<select name=&quot;probcat&quot;>
<%
Do Until rs.EOF
  Response.Write &quot;<option value='&quot; & rs(&quot;col1&quot;) & &quot;'>&quot; & rs&quot;col1&quot;) & &quot;</option>&quot;

  rs.MoveNext
Loop
%>
</select>
...

<%
'close rs and conn
%>
--James
 
Just some more advice, whenever I have to build an option list like this, I use the getstring() method. It can perform better and also allows you to close you db connections as quickly as possible:

Code:
<%
'open conn

Set rs = objConn.Execute(strSQL)

strPrefix = &quot;<option value='&quot;
strMidfix = &quot;'>&quot;
strSuffix = &quot;</option>&quot;

strOptions = rs.GetString(,,strMidfix,strSuffix & strPrefix)
strOptions = strPrefix & Left(strOptions, Len(strOptions) - Len(strPrefix))

'we can now close the recordset and connection as we have the data in our string variable
%>

...
<select name=&quot;probcat&quot;><%= strOptions %></select>
...

You can view a more detailed breakdown of the getstring() method here:
--James
 
You guys are the best. Thanks for all the comments and advice. All is much appreciated. may you all have a great day. Cheers,
 
I use this class to optimize my string concatenations.

Code:
'********************************************************************************
' START STRING OPTIMIZER CLASS
'********************************************************************************

' PURPOSE: 	This is a string optimization class when working with large
'						string concatenations. This class will dramatically speed
'						the process of concatenating strings.
' 			
' ABOUT: 	
'			VB and VBScript have intrinsic array functions such as JOIN that are, 
'			MUCH FASTER at concatenating variant array elements. This class keeps our 
'			string information in an array (its always a Variant anyway, so what's the 
'			difference?) and then when we're finished with all of our concatenations we 
'			have it just do a JOIN on the array with NO DELIMITER so it all comes back 
'			as ONE BIG LONG STRING, in a SINGLE OPERATION.
'
' USAGE:	
'			Set objStrOpt = New StringOptimizer
'			objStrOpt.Reset
'			objStrOpt.Append &quot;String1&quot;
'			objStrOpt.Append &quot;String2&quot;
'			objStrOpt.Append &quot;Stringx&quot;
'			Response.write(objStrOpt.Concat)
Class StringOptimizer
	Dim stringArray,growthRate,numItems

	Private Sub Class_Initialize()
		growthRate = 50: numItems = 0
		ReDim stringArray(growthRate)
	End Sub

	Public Sub Append(ByVal strValue)
		' next line prevents type mismatch error if strValue is null. Performance hit is negligible.
		strValue=strValue & &quot;&quot;
	
		If numItems > UBound(stringArray) Then ReDim Preserve stringArray(UBound(stringArray) + growthRate)
		stringArray(numItems) = strValue: numItems = numItems + 1

	End Sub

	Public Sub Reset
		Erase stringArray
		Class_Initialize
	End Sub

	Public Function Concat() 
		Redim Preserve stringArray(numItems) 
		Concat = Join(stringArray, &quot;&quot;)
	End Function

End Class
_______________________________
regards,
Brian
AOL IM: FreelanceGaines
AG00280_.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top