I have been banging my head on this one for a couple days now. I am upgrading my ASP.NET application to utilize Bootstrap and jQuery. In the past, all worked well. An INSERT
only inserted 1 record. Now, I get 2 duplicate records (simultaneous insert?) every time I submit my add function. I cannot figure out why?
Here is my stored procedure:
ALTER PROCEDURE [dbo].[fs_AddQuickExist]
@cid integer,
@shid integer
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION
--ASSIGN OTHER VARIABLES
DECLARE @dst datetime
SET @dst = GetDate()
-- ADD THE EXISTING SHOOTER TO THE CLUB
INSERT INTO shooter_club
VALUES (@shid, @cid, 0, NULL, NULL)
COMMIT TRANSACTION
SET NOCOUNT OFF;
END
I simply execute (EXEC) it from my ASP.NET page after a post. I tested using javascript with an "alert" box - and it seems the page only loads once.?
Thank you in advance...
UPDATE: I ran SQL Profiler and found that it is running the SP twice. So - here is my ASP code (in simple form))
<html>
<!--#include file="../../systemwide/dbParams.asp"-->
<link href="../../systemwide/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../systemwide/datetimepicker/css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="../../systemwide/scripts/jquery-2.1.4.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="../../systemwide/bootstrap/js/bootstrap.min.js"></script>
<head>
<%
theCID = request.querystring("cid")
theSID = request.querystring("sid")
theEID = request.querystring("eid")
theQID = request.querystring("qid")
theFNM = request.querystring("fn")
theLNM = request.querystring("ln")
set cn=server.createobject("adodb.connection")
cn.open dbSource
set rs = cn.execute("EXEC fs_CheckQuickAdd @fn=" & request("fn") & ", @ln=" & request("ln"))
%>
<script language="JavaScript1.2">
function addExist(id) {
document.quick.shid.value=id;
document.quick.fct.value="AX";
document.quick.submit();
}
function addNew() {
document.quick.fct.value="AN";
document.quick.submit();
}
</script>
</head>
<body onload="initDoc(document.canvas);" data-spy="scroll" data-offset="50">
<form name="quick" method="post" action="squad.asp">
<input type="hidden" name="cid" value="<%=theCID%>">
<input type="hidden" name="sid" value="<%=theSID%>">
<input type="hidden" name="qid" value="<%=theQID%>">
<input type="hidden" name="eid" value="<%=theEID%>">
<input type="hidden" name="fn" value="<%=theFNM%>">
<input type="hidden" name="ln" value="<%=theLNM%>">
<input type="hidden" name="shid" value="">
<input type="hidden" name="fct" value="">
<div class="container">
<h3><b>Shooter Quick Add</b></h3>
<p>The system has located some possible matches for your new shooter. In an effort to keep this database fast and clean, please check this list below to see if one of
these shooters is the one that you are adding to your club inventory.</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th> </th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>City</th>
</tr>
</thead>
<tbody>
<%do until rs.eof%>
<tr>
<td><button onclick='addExist(<%=rs("id")%>);' title="Select This Shooter" class='btn btn-success btn-sm btn-inline'><span class='glyphicon glyphicon-check'></span></button></td>
<td><%=rs("fname")%></td>
<td><%=rs("lname")%></td>
<td><%=rs("address")%></td>
<td><%=rs("city")%></td>
</tr>
<%rs.movenext
loop
rs.close%>
<tr>
<td><button onclick='addNew();' title="Add The New Entry" class='btn btn-primary btn-sm btn-inline'><span class='glyphicon glyphicon-plus'></span></button></td>
<td colspan="4">
<p>The shooter that I'm trying to add is not in this list, please just add my entry.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</body>
<%
set rs=nothing
cn.close
set cn=nothing
%>
</html>
...this posts to a different asp page where the function to call the SP is located.
I may also add that I never had this problem till I started using AJAX & jQuery to populate fields in a page.
There's nothing in the procedure listed that would insert two rows.
I'd fire up SQL Profiler and watch while the work is being done. If the procedure is being called twice, you'll see it there. (I think multiple calls is the most likely situation.)
Other (and pretty tenuous) things to check:
begin transaction / commit
. It servers no purpose, and (depending on the complexity of your system) it's just barely conceivable that it is causing weird complications. Your HTML is:
<button onclick='addNew();' ...>
and the addNew()
function does this:
function addNew() {
document.quick.fct.value="AN";
document.quick.submit();
}
The problem is that <button>
is by default of type "submit" so the form is being submitted twice: you submit it in the JavaScript function, and the browser submits it as the default action. Two requests to the server, and the database insertion is therefore run twice.
The solution is to either remove both lines that read document.quick.submit();
or change the button type ( <button type="button">
) for both buttons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.