set interval of 4 second between entry using sql

Analyst_SQL 3,551 Reputation points
2021-06-14T11:32:34.887+00:00

Currently i am facing issue ,of double entry on one button click .

below is code

SQL

ALTER PROCEDURE [dbo].[spInsertPorderder]   
	-- Add the parameters for the stored procedure here  
	@Codeitem int,  
	@OrderNo int,  
	@prdqty int,  
	@IDWoker int,  
	@EntryDate date,  
	@FID int,  
  
	@Weight int,  
	@SecID int,  
	@Rmk varchar (50),  
	@ETime time(7),  
	@BrefNo varchar (50),  
	@IPAddress [varchar](50) ,  
	@Gweigth int,  
	@OID int output  
	   
	  
AS  
BEGIN  
	  
	SET NOCOUNT ON;  
  
  
		IF NOT EXISTS   
(  
   SELECT ETIME, IPAddress,EntryDate  FROM Probale  
   WHERE ETIME = @ETIME and IPAddress = @IPAddress and EntryDate=@EntryDate and Codeitem=@Codeitem  
)  
Begin   
		insert into Probale(Codeitem,OrderNo,prdqty,IDWokrer,EntryDate,FID,Weigth,SecID,Rmk,ETime,BrefNo,IPAddress,Gweigth )   
	values(@Codeitem,@OrderNo,@prdqty,@IDWoker,@EntryDate,@FID,@Weight,@SecID,@Rmk,@ETime,@BrefNo,@IPAddress,@Gweigth )  
	set @OID=SCOPE_IDENTITY()  
end  
end  

Button Behind Code attached
105395-double.txt

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,388 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,586 questions
{count} votes

Accepted answer
  1. Michael Taylor 50,431 Reputation points
    2021-06-14T14:46:18.783+00:00

    As @Tom Phillips mentions, each time your sproc is called you're going to insert a new row most likely. Your IF statement will rarely be true and is pretty much useless. Example: if you call this sproc twice in a row then in all probability there will be at least a half second difference. Therefore when you query to see if there is already a row in the table you'll be using the current time and half a second later you'll query again but the time will have changed so you'll get a new row each time.

    Personally the issue is that your app is sending multiple requests and it shouldn't. This is common in web apps because the user in the browser can click a button any # of times. Unless your app is disabling that button via JS as soon as it clicked then the user can click it again. Since web apps are stateless you'll get a brand new request sent to the server again asking you to repeat the process. Hence you get 2 requests. This is how web apps work. If the request cannot support concurrent requests then you have to programmatically handle it. How you do that is completely reliant on what you're doing and how. For example creating a new resource generally means the second request will fail because the resource has already been created. However if this were an update request (and you're not using concurrency detection) then the resource would be updated twice (which shouldn't matter in most cases).

    I believe the solution to your problem isn't in fixing the SQL but fixing your app to prevent duplicate requests by the user.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,721 Reputation points
    2021-06-14T13:48:50.627+00:00

    Your problem is your are using
    WHERE ETIME = @ETIME

    and @ETIME is the current date time, so that is always going to be different. You need an actual primary key in your table.


  2. Suwandi 1 Reputation point
    2021-06-19T15:57:49.923+00:00

    Use DateTime column inserttime to save current datetime
    Then
    Check if not exist(select * from probale where dateadd(second,4,inserttime)>getdate())