Comma Separated String into a drop down in classic asp - asp-classic

I have a comma separated string
say 12345,67890,3453,124556,56778
and I want to display these items in a drop down list
I am working in a classic asp page.
Please help me on this

Give this a try:
<%
' put your string into a variable.
Dim myString : myString = "12345,67890,3453,124556,56778"
' split your variable up into a array
Dim splitmystring : splitmystring = split(myString,",")
' create a dropdown box
Response.write "<select value=""dropdown"">"
Response.write "<option selected>choose a option</option>"
' Loop through your array
For Each item In splitmystring
response.write "<option value='"& item &"'>"& item & "</option>"
Next
'close your dropdown box
response.write "</select>"
%>

Related

How can I add/update recordset based on dynamic posted input fields? [duplicate]

on submit of a form, I would like to capture the field names and values of the forms and I want them passed without even showing in the browser (Response.Write makes them visible in the browser). How I can do this please? I am using this code:
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(""& fieldName &" = Request.Form("""& fieldName &""")")
Next
Your code is essentially correct, so just remove the Response.Write and do something else with the fieldName and fieldValue variables you're populating. After you're done with manipulating the data (either inserting it into a database or sending an e-mail), you can redirect the user to a success / thank you page.
To test that you're receiving the correct input, you can change your Response.Write to
Response.Write fieldName & " = " & fieldValue & "<br>"
Update
Here's how you could use a Dictionary Object to put your field names and field values together:
Dim Item, fieldName, fieldValue
Dim a, b, c, d
Set d = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
d.Add fieldName, fieldValue
Next
' Rest of the code is for going through the Dictionary
a = d.Keys ' Field names '
b = d.Items ' Field values '
For c = 0 To d.Count - 1
Response.Write a(c) & " = " & b(c)
Response.Write "<br>"
Next
This is a very small snippet I use to show all form fields POSTED
<%
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
%>
To complement the answer, and reply to the second question #ReneZammit asked in his comment from Jan 31, 2012 at 10:17, Creating "dynamic" ASP/VBScript variables based on the field names IS POSSIBLE!
You just have to use the "magical" Execute() function :
<%
'If the form was SUBMITTED
If (Request.Form("submitForm") = "1") then
Dim fieldName
Dim fieldValue
'Loop through all the form items
For Each Item In Request.Form
'Get Form item properties
fieldName = Item
fieldValue = Request.Form(Item)
'Use Execute() to interpret dynamically created ASP code
Execute("Dim myVar_" & fieldName)
Execute("myVar_" & fieldName & " = """ & fieldValue & """")
Next
'Use your new Dynamic variables Names, that are now REAL native variables names!
Response.Write "<br>myVar_banana = " & myVar_banana
Response.Write "<br>myVar_pear = " & myVar_pear
Response.Write "<br>myVar_tomato = " & myVar_tomato
'If the form have to be DISPLAYED
else
%>
<form method="post">
<input type="text" name="banana" value="This is Yellow">
<br>
<input type="text" name="pear" value="This may be Green">
<br>
<input type="text" name="tomato" value="This should be Red">
<input type="hidden" name="submitForm" value="1">
<input type="submit">
</form>
<%
end if
%>
More information available here : http://www.aspdev.org/asp/asp-eval-execute/

Exclude certain selections from a drop down list ASP

I'm working on a ASP (Within the payment gateway). The options in the drop down at being pulling in from a database. I can't touch the database so have to attack the matter from the code.
Here is the value name that I need to exclude _01BM(Q)
Here is the code for the drop down.
<select name="programgroup" onchange="onProgramGroup()">
<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False)%>
</select>
I would really appreciate any help on this guys.
Here is the code for the method:
Sub buildDropDownList(strCurrentSelection, objListData, strCodeName, strDescriptionName, blnIncludeOther)
If Not objListData.BOF Then
objListData.MoveFirst
End If
While Not objListData.EOF
Response.Write "<option value='" & objListData(strCodeName) & "' "
If StrComp(strCurrentSelection, objListData(strCodeName),1) = 0 then
Response.Write "selected"
End If
Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
objListData.MoveNext
Wend
if blnIncludeOther then
Response.Write "<option value='<Other>' "
If strCurrentSelection <> "" and InStr(1, "<Other>", strCurrentSelection) = 1 then
Response.Write "selected"
End If
Response.Write ">Other</option>" & VbCrLf
end if
End Sub
You will have to change the method building the drop down. Since you did not provide us with the code for it, I'll give you a skeleton, and can use it to change your actual code.
To make it more generic and less ugly, better pass the value(s) to exclude to the method, as an array, instead of hard coding them in there.
So, the method should look like this:
Sub buildDropDownList(strProgramGroupCode, rsProgramGroup, someParamHere, anotherParam, boolParam, arrValuesToExclude)
Dim excludedValuesMapping, x
Set excludedValuesMapping = Server.CreateObject("Scripting.Dictionary")
For x=0 To UBound(arrValuesToExclude)
excludedValuesMapping.Add(LCase(arrValuesToExclude(x)), True)
Next
'...unknown code here...
Do Until rsProgramGroup.EOF
strCurrentValue = rsProgramGroup(valueFieldName)
If excludedValuesMapping.Exists(LCase(strCurrentValue)) Then
'value should be excluded, you can do something here, but not write it to browser
Else
strCurrentText = rsProgramGroup(textFieldName)
Response.Write("<option value=""" & Replace(strCurrentValue, """", """) & """>" & strCurrentText & "</option>")
End If
rsProgramGroup.MoveNext
Loop
End Sub
And to use it:
<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False, Array("_01BM(Q)"))%>
Now having your code, and if you don't want to make it generic, you can also ignore specific values by having such code in the buildDropDownList sub:
Dim currentCodeValue
While Not objListData.EOF
currentCodeValue = objListData(strCodeName)
If (UCase(currentCodeValue)<>"_04GIDBM") And _
(UCase(currentCodeValue)<>"_05GIDFM") And _
(UCase(currentCodeValue)<>"_08EXHRM") And _
(UCase(currentCodeValue)<>"_10EXMKT") And _
(UCase(currentCodeValue)<>"_12EXTTH") And _
(UCase(currentCodeValue)<>"_17EXHSC") Then
Response.Write "<option value='" & currentCodeValue & "' "
If StrComp(strCurrentSelection, currentCodeValue, 1) = 0 then
Response.Write "selected"
End If
Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
End If
objListData.MoveNext
Wend
This will just skip any records with such values, and will not output a drop down option for them.

Looping through a form to get field names and filed values issue (classic ASP)

on submit of a form, I would like to capture the field names and values of the forms and I want them passed without even showing in the browser (Response.Write makes them visible in the browser). How I can do this please? I am using this code:
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(""& fieldName &" = Request.Form("""& fieldName &""")")
Next
Your code is essentially correct, so just remove the Response.Write and do something else with the fieldName and fieldValue variables you're populating. After you're done with manipulating the data (either inserting it into a database or sending an e-mail), you can redirect the user to a success / thank you page.
To test that you're receiving the correct input, you can change your Response.Write to
Response.Write fieldName & " = " & fieldValue & "<br>"
Update
Here's how you could use a Dictionary Object to put your field names and field values together:
Dim Item, fieldName, fieldValue
Dim a, b, c, d
Set d = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
d.Add fieldName, fieldValue
Next
' Rest of the code is for going through the Dictionary
a = d.Keys ' Field names '
b = d.Items ' Field values '
For c = 0 To d.Count - 1
Response.Write a(c) & " = " & b(c)
Response.Write "<br>"
Next
This is a very small snippet I use to show all form fields POSTED
<%
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
%>
To complement the answer, and reply to the second question #ReneZammit asked in his comment from Jan 31, 2012 at 10:17, Creating "dynamic" ASP/VBScript variables based on the field names IS POSSIBLE!
You just have to use the "magical" Execute() function :
<%
'If the form was SUBMITTED
If (Request.Form("submitForm") = "1") then
Dim fieldName
Dim fieldValue
'Loop through all the form items
For Each Item In Request.Form
'Get Form item properties
fieldName = Item
fieldValue = Request.Form(Item)
'Use Execute() to interpret dynamically created ASP code
Execute("Dim myVar_" & fieldName)
Execute("myVar_" & fieldName & " = """ & fieldValue & """")
Next
'Use your new Dynamic variables Names, that are now REAL native variables names!
Response.Write "<br>myVar_banana = " & myVar_banana
Response.Write "<br>myVar_pear = " & myVar_pear
Response.Write "<br>myVar_tomato = " & myVar_tomato
'If the form have to be DISPLAYED
else
%>
<form method="post">
<input type="text" name="banana" value="This is Yellow">
<br>
<input type="text" name="pear" value="This may be Green">
<br>
<input type="text" name="tomato" value="This should be Red">
<input type="hidden" name="submitForm" value="1">
<input type="submit">
</form>
<%
end if
%>
More information available here : http://www.aspdev.org/asp/asp-eval-execute/

Returning more than 1000 rows in classic asp adodb.recordset

My code in asp classic, doing a mssql database query:
rs.pagesize = 1000 ' this should enable paging
rs.maxrecords = 0 ' 0 = unlimited maxrecords
response.write "hello world 1<br>"
rs.open strSql, conn
response.write "hello world 2<br>"
My output when there are fewer than 1000 rows returned is good. More than 1000 rows and I don't get the "hello world 2".
I thought that setting pagesize sets up paging and thus allows all rows to be returned regardless of how many rows there are. Without setting pagesize, paging is not enable and the limit is 1000 rows. However my page is acting as if pagesize is not working at all.
Please advise.
is it possible you are declaring your oRS.pagesize before you are opening the recordset?
Here is a good example of paging using getrows...
<!--VB ADO Constants file. Needed for the ad... constants we use-->
<!-- #include file="adovbs.inc" -->
<%
' BEGIN USER CONSTANTS
Dim CONN_STRING
Dim CONN_USER
Dim CONN_PASS
' I'm using a DSN-less connection.
' To use a DSN, the format is shown on the next line:
'CONN_STRING = "DSN=DSNName;"
CONN_STRING = "DBQ=" & Server.MapPath("database.mdb") & ";"
CONN_STRING = CONN_STRING & "Driver={Microsoft Access Driver (*.mdb)};"
' This DB is unsecured, o/w you'd need to specify something here
CONN_USER = ""
CONN_PASS = ""
' Our SQL code - overriding values we just set
' Comment out to use Access
CONN_STRING = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
& "Initial Catalog=samples;Connect Timeout=15;" _
& "Network Library=dbmssocn;"
CONN_USER = "samples"
CONN_PASS = "password"
' END USER CONSTANTS
' BEGIN RUNTIME CODE
' Declare our vars
Dim iPageSize 'How big our pages are
Dim iPageCount 'The number of pages we get back
Dim iPageCurrent 'The page we want to show
Dim strOrderBy 'A fake parameter used to illustrate passing them
Dim strSQL 'SQL command to execute
Dim objPagingConn 'The ADODB connection object
Dim objPagingRS 'The ADODB recordset object
Dim iRecordsShown 'Loop controller for displaying just iPageSize records
Dim I 'Standard looping var
' Get parameters
iPageSize = 10 ' You could easily allow users to change this
' Retrieve page to show or default to 1
If Request.QueryString("page") = "" Then
iPageCurrent = 1
Else
iPageCurrent = CInt(Request.QueryString("page"))
End If
' If you're doing this script with a search or something
' you'll need to pass the sql from page to page. I'm just
' paging through the entire table so I just hard coded it.
' What you show is irrelevant to the point of the sample.
'strSQL = "SELECT * FROM sample ORDER BY id;"
' Sept 30, 1999: Code Change
' Based on the non stop questions about how to pass parameters
' from page to page, I'm implementing it so I can stop answering
' the question of how to do it. I personally think this should
' be done based on the specific situation and is clearer if done
' in the same method on all pages, but it's really up to you.
' I'm going to be passing the ORDER BY parameter for illustration.
' This is where you read in parameters you'll need for your query.
' Read in order or default to id
'If Request.QueryString("order") = "" Then
' strOrderBy = "id"
'Else
' strOrderBy = Replace(Request.QueryString("order"), "'", "''")
'End If
' Make sure the input is one of our fields.
strOrderBy = LCase(Request.QueryString("order"))
Select Case strOrderBy
Case "last_name", "first_name", "sales"
' A little pointless, but...
strOrderBy = strOrderBy
Case Else
strOrderBy = "id"
End Select
' Build our SQL String using the parameters we just got.
strSQL = "SELECT * FROM sample ORDER BY " & strOrderBy & ";"
' Some lines I used while writing to debug... uh "test", yeah that's it!
' Left them FYI.
'strSQL = "SELECT * FROM sample WHERE id=1234 ORDER BY id;"
'strSQL = "SELECT * FROM sample;"
'Response.Write "SQL Query: " & strSQL & "<BR>" & vbCrLf
' Now we finally get to the DB work...
' Create and open our connection
Set objPagingConn = Server.CreateObject("ADODB.Connection")
objPagingConn.Open CONN_STRING, CONN_USER, CONN_PASS
' Create recordset and set the page size
Set objPagingRS = Server.CreateObject("ADODB.Recordset")
objPagingRS.PageSize = iPageSize
' You can change other settings as with any RS
'objPagingRS.CursorLocation = adUseClient
objPagingRS.CacheSize = iPageSize
' Open RS
objPagingRS.Open strSQL, objPagingConn, adOpenStatic, adLockReadOnly, adCmdText
' Get the count of the pages using the given page size
iPageCount = objPagingRS.PageCount
' If the request page falls outside the acceptable range,
' give them the closest match (1 or max)
If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
If iPageCurrent < 1 Then iPageCurrent = 1
' Check page count to prevent bombing when zero results are returned!
If iPageCount = 0 Then
Response.Write "No records found!"
Else
' Move to the selected page
objPagingRS.AbsolutePage = iPageCurrent
' Start output with a page x of n line
%>
<p>
<font size="+1">Page <strong><%= iPageCurrent %></strong>
of <strong><%= iPageCount %></strong></font>
</p>
<%
' Spacing
Response.Write vbCrLf
' Continue with a title row in our table
Response.Write "<table border=""1"">" & vbCrLf
' Show field names in the top row
Response.Write vbTab & "<tr>" & vbCrLf
For I = 0 To objPagingRS.Fields.Count - 1
Response.Write vbTab & vbTab & "<th>"
Response.Write objPagingRS.Fields(I).Name
Response.Write "</th>" & vbCrLf
Next 'I
Response.Write vbTab & "</tr>" & vbCrLf
' Loop through our records and ouput 1 row per record
iRecordsShown = 0
Do While iRecordsShown < iPageSize And Not objPagingRS.EOF
Response.Write vbTab & "<tr>" & vbCrLf
For I = 0 To objPagingRS.Fields.Count - 1
Response.Write vbTab & vbTab & "<td>"
Response.Write objPagingRS.Fields(I)
Response.Write "</td>" & vbCrLf
Next 'I
Response.Write vbTab & "</tr>" & vbCrLf
' Increment the number of records we've shown
iRecordsShown = iRecordsShown + 1
' Can't forget to move to the next record!
objPagingRS.MoveNext
Loop
' All done - close table
Response.Write "</table>" & vbCrLf
End If
' Close DB objects and free variables
objPagingRS.Close
Set objPagingRS = Nothing
objPagingConn.Close
Set objPagingConn = Nothing
' Show "previous" and "next" page links which pass the page to view
' and any parameters needed to rebuild the query. You could just as
' easily use a form but you'll need to change the lines that read
' the info back in at the top of the script.
If iPageCurrent > 1 Then
%>
[<< Prev]
<%
End If
' You can also show page numbers:
For I = 1 To iPageCount
If I = iPageCurrent Then
%>
<%= I %>
<%
Else
%>
<%= I %>
<%
End If
Next 'I
If iPageCurrent < iPageCount Then
%>
[Next >>]
<%
End If
' END RUNTIME CODE
%>
Try changing your rs.open line to:
rs.Open strSQL, Conn, 3, 1, &H0001
Here's the breakdown of the function call and parameters:
recordsetobject.Open Source, ActiveConnection, CursorType, LockType, Options
3 - adOpenStatic
1 - adLockReadOnly
&H0001 - adCmdText
I pull this from some old code of mine. I don't remember why this combination of parameters is necessary but it's what is necessary to implement paging.
Unlimited records sounds great but I would set a limit even if it's quite hi.
If you are not getting the "Hello World 2" output, is there an error? That would be helpful as well.
paging is for just that, paging.
your code here is not enough to accomplish that but regardless of the code, why are you trying to return a 1000 rows of data ????
nobody's going to read a 1000 rows of data and it will likely be very slow performance.
Using the following code, I returned 4000+ rows from a sql server table in classic ASP. It's not using the same method, but it doesn't suffer the limitations either.
strconnect = "DRIVER={SQL Server};SERVER=****;DATABASE=****;UID=****;PWD=****"
set conn=server.createobject("adodb.connection")
conn.open strconnect
set rs = conn.execute("select firstname from users")
if not rs.eof then
f_Array = rs.getrows
end if
rs.close
set rs = nothing
conn.close
set conn = nothing
for x = 0 to ubound(f_Array, 2)
response.write (x+1) & ". " & f_Array(0,x) & "<br />"
next

How to display data from stored procedure in a drop down in Classic ASP?

This is related to Classic ASP code.
A page fetches data for a particular ProjectCode.
There is a general text field which shows the Site-Location for selected Project-Code. I want to change it to drop down, so that a user can change the Site-Location from options available (fetched from DB) and then save it. Also, on page load the Site-Location of Project-Code for that particular entry should be selected.
I have added following code to my Page, but it doesn't work(definately I am new to classic ASP).
strSQL = "SP_GET_SiteLocation"
Set rsSiteList = RunSQLQuery(strSQL)
'show the list
If Not rsSiteList.EOF Then
Do While NOT rs.EOF
SiteLocationList= SiteLocationList & "<option value="">" & rs("LOCATION") & "</option>"
rs.MoveNext
Also, on click of save button, i have to send the selected drop down value to update query.
You use a wrong name for the recordset variable..
You named it rsSiteList but you use it as rs
Do While NOT rsSiteList.EOF
SiteLocationList= SiteLocationList & "<option value="">" & rsSiteList("LOCATION") & "</option>"
rsSiteList.MoveNext
Update
You are building a string with all the options ..
you should write it in the page at some point.. response.write(SiteLocationList)
or write the <options> directly to the page..
<select name="somename"><%
Do While NOT rsSiteList.EOF
%>
<option value=""><%=rsSiteList("LOCATION")%></option>
<%
rsSiteList.MoveNext
Loop
%>
</select>
update 2
Not sure why you do not want to print the options as you read them from the recordset but prefer to make a huge string instead and print that at the end ... it is the same thing but much more cleaner ..
The following should select the location that matches the rsReqDetails("AppReqSiteID")
<td>
<%
strSQL="SP_EPAPM_GET_SiteLocation"
Set rsSiteList=RunSQLQuery(strSQL)
selectedValue = rsReqDetails("AppReqSiteID")
If Not rsSiteList.EOF Then
Do While NOT rsSiteList.EOF
loc = rsSiteList("LOCATION")
if loc <> selectedValue then
optionOpen = "<option>"
else
optionOpen = "<option selected=""selected"">"
end if
optionClose = "</option>"
SiteLocationList=SiteLocationList & optionOpen & rsSiteList("LOCATION") & optionClose
rsSiteList.MoveNext
Loop
End If
%>
<select id="SiteLocationList" NAME="SiteLocationList">
<%response.write(SiteLocationList)%>
</select>
</td>
In general you need to watch the nesting of html as it can mess everything up. Also you need to read a little on the interactions between ASP and HTML ...
You almost got it, you're missing the "select" tag:
strSQL = "SP_GET_SiteLocation"
Set rsSiteList = RunSQLQuery(strSQL)
'show the list
If Not rsSiteList.EOF Then %>
<select><%Do While NOT rs.EOF SiteLocationList= SiteLocationList & "<option value="">" & rs("LOCATION") & "</option>"
rs.MoveNext %>
</select>

Resources