[User questions]
In ASP, how do I extract all the values in <select multiple name = "myfunction">
<Select name = "select" size = "10" multiple>
<Option value = "Dataset 1"> Dataset 1 </option>
<Option value = "Dataset 2"> dataset 3 </option>
<Option value = "dataset 3"> dataset 3 </option>
<Option value = "...">... </option>
</SELECT>
This is a list box that allows you to select multiple data items in the list box. The problem is:
How can we submit multiple values in the list and obtain the result set, and use SQL statements to query the submitted values? Thank you for your help!
[Griefforyou answer]
After multiple entries are selected, the data is automatically separated by commas.
[User questions]
Thank you for your reply. I use the request. form ("select") cannot pass multiple selected result sets. Besides, I want to use the data values selected in the list for select queries. If multiple values are selected in the list, how to obtain the value and how to use it for query? Can this be an example? Hope again! Thank you!
[Griefforyou answer]
<%
If request. Form ("action") <> "then
SQL = "select * from table where fieldname in ('" & replace (request. Form ("select "),",","','")&"')"
Response. Write SQL
End if
%>
<Form name = "form1" method = "Post">
<Select name = "select" multiple>
<Option value = "1"> --- 1 --- </option>
<Option value = "2"> --- 2 --- </option>
<Option value = "3"> --- 3 --- </option>
<Option value = "4"> --- 4 --- </option>
<Option value = "5"> --- 5 --- </option>
</SELECT>
<Input type = "hidden" name = "action" value = "">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>
I select 2, 3, and the generated SQL is
Select * from table where fieldname in ('2', '3 ')
[User questions]
It is feasible, but because I need to use a stored procedure, for example, there is no problem when selecting a project in the list, but there will be problems when there are more than two, as prompted:
Microsoft ole db provider for SQL Server Error '80040e14' must pass 2 parameters and pass subsequent parameters in the form of '@ name = value. Once the form of '@ name = value' is used, all subsequent parameters must be passed in the form of' @ name = value.
When using the Stored Procedure select * from table where @ fieldname = ('"& replace (request. form ("select"), "','") & "')" An error occurs during query, the reason is that @ fieldname cannot assign multiple values to variables in the stored procedure at a time. Only one value is allowed.
In this case, you may need to save multiple selected list values as variables or arrays? But how to implement it? Could you please help me to answer this question ?!
[Griefforyou answer]
1. process numeric fields
A. Stored Procedure
Create procedure sp_test (
@ ID: varchar (1000)
)
Declare @ SQL nvarchar (2000)
Set @ SQL = n' select * from book where ID in ('+ @ ID +') '-- generate an SQL statement
Exec sp_executesql @ SQL
Go
B. ASP program
<%
Dim connstr
Dim CN, CMD, RS
Dim ID
If request. Form ("action") <> "then
'Obtain the query conditions submitted by the user
Id = Replace (request. Form ("select"), ") 'with no single quotation marks
'Connection string
Connstr = "provider = sqloledb.1; Password = sa; persist Security info = true; user id = sa; initial catalog = book; Data Source = (local )"
'Create a connection object
Set Cn = server. Createobject ("ADODB. Connection ")
'Connect to the database
CN. Open connstr
'Connection command object
Set cmd = server. Createobject ("ADODB. Command ")
'Set the connection of the command object
Cmd. activeconnection = Cn
'Set the command type to Stored Procedure
Cmd. commandtype = 4
'Specifies the stored procedure name
Cmd. commandtext = "sp_test"
'Set stored procedure parameters
Cmd. Parameters. append cmd. createparameter ("@ ID", 1000)
CMD ("@ ID") = id' directly assigns a value to the parameters of the stored procedure
'Save Stored Procedure returned record set
Set rs = cmd. Execute
While not Rs. EOF
Response. Write RS ("bookname") & "" & RS ("Writer") & "<br>"
Rs. movenext
Wend
Rs. Close
Set rs = nothing
Set cmd = nothing
CN. Close
Set Cn = nothing
End if
%>
<Form name = "form1" method = "Post">
<Select name = "select" multiple>
<Option value = "217"> --- 217 --- </option>
<Option value = "218"> --- 218 --- </option>
<Option value = "3"> --- 3 --- </option>
<Option value = "4"> --- 4 --- </option>
<Option value = "5"> --- 5 --- </option>
</SELECT>
<Input type = "hidden" name = "action" value = "">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>
2. Field Processing
A. Stored Procedure
Create procedure sp_test2 (
@ Writer varchar (1000)
)
Declare @ SQL nvarchar (2000)
Set @ SQL = n' select * from book where writer in ('+ @ writer +') '-- generate an SQL statement
Exec sp_executesql @ SQL
Go
B. ASP program
<%
Dim connstr
Dim CN, CMD, RS
Dim writer
If request. Form ("action") <> "then
'Obtain the query conditions submitted by the user
Writer = Replace (request. Form ("select"), "','") 'with single quotation marks
'Connection string
Connstr = "provider = sqloledb.1; Password = sa; persist Security info = true; user id = sa; initial catalog = book; Data Source = (local )"
'Create a connection object
Set Cn = server. Createobject ("ADODB. Connection ")
'Connect to the database
CN. Open connstr
'Connection command object
Set cmd = server. Createobject ("ADODB. Command ")
'Set the connection of the command object
Cmd. activeconnection = Cn
'Set the command type to Stored Procedure
Cmd. commandtype = 4
'Specifies the stored procedure name
Cmd. commandtext = "sp_test2"
'Set stored procedure parameters
Cmd. Parameters. append cmd. createparameter ("@ writer", 1000)
CMD ("@ writer") = "'" & writer & "'" 'adds a single quotation mark to both sides, and then assigns a value to the parameters of the stored procedure.
'Save Stored Procedure returned record set
Set rs = cmd. Execute
While not Rs. EOF
Response. Write RS ("bookname") & "" & RS ("Writer") & "<br>"
Rs. movenext
Wend
Rs. Close
Set rs = nothing
Set cmd = nothing
CN. Close
Set Cn = nothing
End if
%>
<Form name = "form1" method = "Post">
<Select name = "select" multiple>
<Option value = "Xi"> --- Xi --- </option>
<Option value = "Karen"> --- Karen --- </option>
<Option value = "3"> --- 3 --- </option>
<Option value = "4"> --- 4 --- </option>
<Option value = "5"> --- 5 --- </option>
</SELECT>
<Input type = "hidden" name = "action" value = "">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>
The above program passed the test in Win2000 + iis5 + SQL Server 2000.