Double entry on one button click ,asp.net c# webform

Analyst_SQL 3,576 Reputation points
2021-06-14T11:42:30.127+00:00

I am currentyl facing issue ,that user click one time on Button ,then entry get inserted two time in system, so i want to set interval of 4 second between entries,mean that if User insert time 11:15:14 ,if user try to save another entry then it take 4 second ,means that next entry save on 11:15:19.

below is code

 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  

105358-double.txt

Developer technologies Transact-SQL
Developer technologies ASP.NET Other
SQL Server Other
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2021-06-15T10:50:20.06+00:00

    As explained in your duplicate thread, use JavaScript and standard ASP.NET properties to disable the submit button. The user will not be able to double click the button.

     OnClientClick="this.disabled = true;"   
     UseSubmitBehavior="false"   
    

    Also, redirect on the server to a confirmation page or the same page. This is called the Post/Redirect/Get pattern and it solve this very common programming problem.


5 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-06-14T14:52:42.957+00:00

    The problem is you are using:
    WHERE ETIME = @ETIME

    and @ETIME is the current datetime. So you are guaranteed duplicate records.

    You need a proper primary key.


  2. Duane Arnold 3,216 Reputation points
    2021-06-14T15:29:27.99+00:00

    Your ASP.NET Webform solution seems to be poorly designed if it is allowing multiple inserts of duplicate data. Also you seem to be posting to the wrong tags to address the issue. There are ASP.NET tags in Q&A you can post to for help.

    0 comments No comments

  3. AgaveJoe 30,126 Reputation points
    2021-06-16T14:07:47.55+00:00

    This is a very common programming problem that has been solved for many years. Unfortunately, it seems like you are not following the recommendations.

    The inserts are a second apart according to your latest screenshot. This indicates a user's double click. Stop the double click as suggested in this thread as well as your duplicate thread. The code is very simple and attached.

    In the event that JavaScript is disabled, you can write basic SQL to detect if this item has been added within the last few seconds. Unfortunately the forum does not allow posting some SQL scripts or tags with JavaScript.

    106291-sqlex.png


  4. Suwandi 1 Reputation point
    2021-06-19T15:47:31.557+00:00

    You can insert getdate() to inserttime column
    Then check if not exist select * where dateadd(second, 4,insertime)>getdate() before inserting.
    You can add additional ipaddress in where condition


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.