Thursday, March 22, 2012

ASP 3.0 Record is getting saved twice.

Hello All,

I am facing this unusual error. In My ASP Page I am submitting the Page and after submitting the form I am calling another ASP page, which contains sql command to insert the record.

My Problem is when I am submitting the Form. Record is getting inserted twice with same values.

What is the cause of this problem as I am facing this problem in every form.

With Regards

Sunil Dutt

First off, you (almost certainly) need to include a UNIQUE constraint to avoid allowing a user interface inserting duplicate data. You probably have a identity value for a primary key.

Second, the fact that I had to say "probably" in the previous statement means we are going to need more information, perhaps code. The post might need to be moved to the ASP forums, but first step, code :)

|||

Setting the Primary key concept I had used but that solved the problem but the asp page shows the error. And for a User it seems as there was error and his record is not saved. I am pasting my Code here :

LoggerDataEntryForm.asp

<%@. Language=VBScript %>
<HTML>
<head>
<title>Inerface Services</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="CSS/New%20Folder.css" rel="stylesheet" type="text/css">
<link href="CSS/IntStyle.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
<!--
function formSave()
{
alert('Saving Record');
var frm = document.getElementById("frmUser_Master");
frm.action = "User_MasterAction.asp";
document.getElementById("frmUser_Master").submit();
}
//-->
</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<form name="frmUser_Master" id="frmUser_Master" method="Post">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="top"><table width="650" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" class="PageBkBorder"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="LogoBk"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" align="center"><img src="images/interface-logo.gif" width="65" height="86"></td>
<td align="left" class="HeadingName"><br>
Web Tracking</td>
</tr>
</table></td>
</tr>
<tr>
<td height="7" class="GreyLine"><img src="images/Spacer.gif" width="1" height="7"></td>
</tr>
<tr>
<td height="20" class="BlueLine"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td class="Toplinks"><a href="LogIssue.htm" class="Toplinks">Log
Issue</a></td>
<td width="7"><img src="images/Divider.gif" width="7" height="19"></td>
<td class="Toplinks"><a href="ListDetails.htm" class="Toplinks">View Issue</a></td>
<td width="7"><img src="images/Divider.gif" width="7" height="19"></td>
<td class="Toplinks"><a href="Summary.htm" class="Toplinks">Summary</a></td>
<td width="7"><img src="images/Divider.gif" width="7" height="19"></td>
<td class="Toplinks"><a href="OpenLastSavedIssue.htm" class="Toplinks">Open
Last Saved issue</a></td>
<td width="7"><img src="images/Divider.gif" width="7" height="19"></td>
<td class="Toplinks"><a href="LoggerDataEntryForm.htm" class="Toplinks">Register
Now! </a></td>
</tr>
</table></td>
</tr>
<tr>
<td height="300" align="center" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="3">
<tr>
<td align="center"><table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" class="PageTitle">Logger Entry Form</td>
</tr>
<tr>
<td align="center" class="FormBk"><table border="0" cellpadding="0" cellspacing="3">
<tr>
<td class="LabelText">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="100" class="LabelText">Name</td>
<td> <input id="txtName" name="txtName" type="text" class="FormText">
</td>
</tr>
<tr>
<td class="LabelText">Designation</td>
<td><input id="txtDesig" name="txtDesig" type="text" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">ID No</td>
<td><input id="txtID" name="txtID" type="text" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">Contact No</td>
<td><input id="txtPhoneNo" name="txtPhoneNo" type="text" class="FormText"></td>
</tr>
<tr>
<td height="21" class="LabelText">Mobile</td>
<td><input id="txtMblPhoneNo" name="txtMblPhoneNo" type="text" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">Username <br>
(Email ID)</td>
<td><input id="txtEmailID" name="txtEmailID" type="text" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">Password</td>
<td><input id="txtPwd" name="txtPwd" type="Password" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">Conform Password</td>
<td><input id="txtConPwd" name="txtConPwd" type="Password" class="FormText"></td>
</tr>
<tr>
<td class="LabelText">Branch Details</td>
<td><table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="SmallText">State</td>
</tr>
<tr>
<td><select name="cmbState">
<option selected>Select All</option>
<option>Andhra Pradesh </option>
<option>Arunachal Pradesh</option>
<option>Assam</option>
<option>Bihar</option>
<option>Chhattisgarh</option>
<option>Goa</option>
<option>Gujarat</option>
<option>Haryana</option>
<option>Himachal Pradesh</option>
<option>Jammu and Kashmir</option>
<option>Jharkhand</option>
<option>Karnataka</option>
<option>Kerala</option>
<option>Madhya Pradesh</option>
<option>Maharashtra</option>
<option>Manipur</option>
<option>Meghalaya</option>
<option>Mizoram</option>
<option>Nagaland</option>
<option>Orissa</option>
<option>Punjab</option>
<option>Rajasthan</option>
<option>Tamil Nadu</option>
<option>Tripura</option>
<option>Uttarakhand</option>
<option>Uttar Pradesh</option>
<option>West Bengal</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td><table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="SmallText">Area</td>
</tr>
<tr>
<td><select name="cmbArea">
<option selected>Select All</option>
<option value="klm1">klm1</option>
<option value="klm2">klm2</option>
<option value="klm3">klm3</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="SmallText">City</td>
</tr>
<tr>
<td><select name="cmbCity">
<option selected value="Select">Select</option>
<option selected value="Pune">Pune</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="SmallText">Branch</td>
</tr>
<tr>
<td><select name="cmbBranch">
<option selected>Select</option>
<option value="mhlm1">mhlm1</option>
<option value="mhlm2">mhlm2</option>
<option value="mhlm3">mhlm3</option>
<option value="mhlm4">mhlm4</option>
<option value="mhlm5">mhlm5</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" onclick=formSave();"></td>
</tr>
<tr>
<td class="LabelText">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>

&nbsp;

</td>
</tr>
</table></td>
</tr>
<tr>
<td height="11" align="center" valign="top" class="LightBlue"><img src="images/Spacer.gif" width="1" height="10"></td>
</tr>
<tr>
<td align="center" valign="top" class="BlueLine">&nbsp;</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</HTML>

User_MasterAction.asp

<%@. Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<!--#include file="dbcon.asp"-->
</HEAD>
<BODY>
<%
dim user_name,user_designation,User_ID,User_PhoneNo,User_MobileNo,User_email,user_password,user_state,user_branch,user_area,user_city
Response.Write "Test"
user_name = Request.Form("txtName") 'Name
user_designation = Request.Form("txtDesig") 'Designation
User_PhoneNo = Request.Form("txtPhoneNo") 'Phone Number
User_MobileNo = Request.Form("txtMblPhoneNo") 'Mobile Number
User_email = Request.Form("txtEmailID") 'Email ID
user_password = Request.Form("txtPwd") 'Password
user_state = Request.Form("cmbState") 'State
user_branch = Request.Form("cmbBranch") 'Branch
user_area = Request.Form("cmbArea") 'Area
user_city = Request.Form("cmbCity") 'City

dim strSql
strSql = "EXEC SP_UserMaster '" & user_name &"'," & user_designation & ",-1,'" & User_PhoneNo &"','" & User_MobileNo & "','"
strSql = strSql & User_email & "','" & user_password &"','" & user_state & "','" & user_branch & "','"
strSql = strSql & user_area & "','" & user_city &"',1"

Set TBRs = Server.CreateObject("ADODB.RecordSet")
Dim NxtUserID
TBRs.Open strSql,objcon ',adOpenStatic,adLockReadOnly
'NxtUserID = TBRs("NxtUserID").Value
%>

User Registration is Done. <a href="LoggerDataEntryForm.asp">Go To User Registration</a>


</BODY>
</HTML>

Stored Procedure SP_UserMaster

CREATE Procedure SP_UserMaster
@.user_name varchar(100),
@.user_designation int,
@.User_ID int,
@.User_PhoneNo varchar(15),
@.User_MobileNo varchar(20),
@.User_email varchar(50),
@.user_password varchar(15),
@.user_state varchar(50),
@.user_branch varchar(50),
@.user_area varchar(50),
@.user_city varchar(50),
@.mode int

As

--@.mode = 0 Edit
--@.mode = 1 Add
--@.mode = 3 Delete

if @.mode = 1
begin
declare @.nextUserID int
select @.nextUserID=ISNull(max([User_ID]) ,0) + 1 from User_Master

Insert into User_Master ([user_name],user_designation,[User_ID],phone_no,mobile_no,email_id,password,state,branch,area,city)
Values
(@.user_name,@.user_designation,@.nextUserID,@.User_PhoneNo,@.User_MobileNo,@.User_email,@.user_password,@.user_state,@.user_branch,@.user_area,@.user_city)

select @.nextUserID as NxtUserID
end
if @.mode = 0
begin
update User_Master set
[user_name] = @.user_name,
user_designation = @.user_designation,
Phone_No = @.User_PhoneNo,
Mobile_No = @.User_MobileNo,
email_id = @.User_email,
[password] = @.user_password,
state = @.user_state,
branch = @.user_branch,
area = @.user_area,
city = @.user_city where [User_ID] = @.User_ID
end
if @.mode = 3
begin
update User_Master set [User_Status] = 3 where [User_ID] = @.User_ID
end
GO

|||Moved to data access forum. Perhaps someone there knows the answer.|||SP looks OK to me. I am suspecting that your page called twice and you get duplicated inserts because of it.|||I just started having the same problem. Were you able to find a solution?

*** edited to add ***

Figured it out. I had the exact same thing in my code, an input type of "submit" as well as an onclick event that also submitted the form. Once I changed the "submit" to a "button", I stopped getting the duplicates.|||

yes, the problem was because the Javascript function formSave is called from the submit button and in submitform the form is submitted again. So the form was getting submitted twice just comment the submit line of the javascript or call the Java Script function FormSave from the link instead of Submit Button.

Regards

Sunil Dutt

No comments:

Post a Comment