• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

SQL Syntax - Insert multiple records with one submission - Classic ASP

Hi Experts,
My query returns multiple records that I would like to submit to a different table with a single submission.
I'm using checkboxes for each record that I would like to insert into my new table.
My "Process.asp" can insert only one record at once. How can I modify it to submit all records at once. Please take a look at my code.
I would really appreciate your help.
<html>
<head>
<title></title>
<link rel="stylesheet" href="/RouteMapper/css/Styles.css" type="text/css" />

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkLikeMe($) {
    var x = document.getElementsByName('DeviceName');
    for (var y = 0; y < x.length; y++)
        x[y].checked = $;
}

// End -->
</script>

</head>
<body>

<form name="contact_form" method="post" action="Process.asp">
<table border="1">
<tr>
<th><input type="checkbox" onClick="checkLikeMe(this.checked)"></th>
<th>Device Name</th>
<th>OU</th>
<th>SubOU</th>
</tr>

<%
rs.AbsolutePage = intPage
For intRecord = 1 To rs.PageSize
%>
<tr>
<td><input type="checkbox" name="DeviceName" value="<%=rs("DeviceName") %>"></td>
<td><%=rs("DeviceName")%></td>
<td><%=rs("OU")%></td>
<td><%=rs("SubOU")%></td>
</tr>



<%
	rs.MoveNext
If rs.EOF Then Exit For

Next

rs.Close
set rs = Nothing
AdoCon.Close
set AdoCon = nothing
%>
</table>

<center>
<br />
<select name="Environment">
<option value="5">Dev</option>
<option value="4">Stage</option>
</select>

<input type="Submit" name="Submit" value="Submit" />
</center>
</form>
</center>

</body>
</html>	

Open in new window




And this is my "Process.asp" that needs to be modified in order to insert multiple records:

<!-- #include virtual="/RouteMapper/Connections/APSConn.asp" -->
<%
'declare variables
Dim AdoCon, strSQL, strDeviceName, strOU, strSubOU, Environment

'Set values into variables
strDeviceName = request.Form("DeviceName")
strOU = request.Form("OU")
strSubOU = request.Form("SubOU")
strEnvironment = request.Form("Environment")

'create SQL string for inserting data into database
strSQL = "INSERT INTO TestTable (DeviceName, OU, SubOU, Environment) VALUES ('"& strDeviceName &"','"& strOU &"','"& strSubOU &"','"& strEnvironment &"')"

'now add data with Execute() method
AdoCon.Execute(strSql)

'close AdoConection object
AdoCon.Close
Set AdoCon = Nothing


'Redirect to the completed.asp page
Response.Redirect "Default.asp"

%>

Open in new window

1
romsom
Asked:
romsom
  • 5
  • 3
2 Solutions
 
Jan LouwerensSoftware EngineerCommented:
You probably want something like:

for index = 1 to Request.Form("DeviceName").Count
   strDeviceName = request.Form("DeviceName")[index]

   'create SQL string for inserting data into database
   strSQL = "INSERT INTO TestTable (DeviceName, OU, SubOU, Environment) VALUES ('"& strDeviceName &"','"& strOU &"','"& strSubOU &"','"& strEnvironment &"')"

   'now add data with Execute() method
   AdoCon.Execute(strSql)
next

Open in new window


Also note that the "DeviceName" is the only parameter you're putting into the form to be harvested. The "OU" and "SubOU" fields are not set into form inputs, so they would not be harvested after the form is submitted.
0
 
romsomAuthor Commented:
Thank you for your quick response. I'm trying to make it work now.
What would be the best way to also harvest OU and SubOU?
0
 
Jan LouwerensSoftware EngineerCommented:
I don't know if there's a single best way, but options include:
  1. Concatenating them together, using a unique delimiter that you know doesn't show up in the data (such as a comma, colon, or pipe symbol)
  2. Making a  JSON object

Whichever method you choose, you have to combine them all together in the form side, and set that as the value of the checkbox input. Then, as you harvest it, you'll have to do the reverse, and split the values apart again.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
romsomAuthor Commented:
I get an error after I submit. Would you know the reason?

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/ewr/lab/Process.asp, line 5

strDeviceName = request.Form("DeviceName")[index]
------------------------------------------^
0
 
Jan LouwerensSoftware EngineerCommented:
Those square brackets should be parenthesis. That was my typo.

strDeviceName = request.Form("DeviceName")(index)

Open in new window

0
 
romsomAuthor Commented:
Thank you, now it inserts the checked records. Unfortunately it doesn't insert "Environment". It just sends the value 0, not the one selected in the dropdown. What can be the reason?
0
 
romsomAuthor Commented:
The problem is fixed, thank you so much for your help!
0
 
romsomAuthor Commented:
Thank you very much, it's working the way I wanted it. I'm really grateful for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now