Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am working on a web project. In my project, I am inserting data into db through button submit. After completion of button click action, if I refresh my page, a duplicate record gets stored in db.

How to prevent duplicate data insert, if i click on refresh?
Posted
Updated 6-Jun-23 0:56am
v2

Have a look at these articles for similar issue:
Refresh Page Issue in ASP.Net[^]
Stop Refresh after Submitting your Request[^]
 
Share this answer
 
Comments
Prerak Patel 20-Sep-11 0:48am    
My 5
[no name] 20-Sep-11 1:00am    
My 5!
There are many ways to check the page is refreshed or not.

You should make the database/store procedure strong to prevent any unnecessary or duplicate entry. As you don't have that much control over UI/client side.[Although there are many ways :) ].

So just place some condition in the database like, if the incoming data is already available then just return o status and inform user that data is already available.

Above all posted solutions are very good and you can also get some idea from it.
 
Share this answer
 
Hi,

One simple thing you can do.

Take procedure and write your insert code into that procedure.
Also put a if condition with record exists.

If record exists that return message that record exists. Else insert a value.
 
Share this answer
 
try this at the end of button_click event
.....
.....
DB.save();

//makesa round-trip of the same page
Response.Redirect(Request.Url.AbsolutePath);
 
Share this answer
 
Hello srinivasvempa,
1) You need change your code as bellow:
C#
if (!IsPostBack)
{
// Your code to insert record into database
}
 
Share this answer
 
Comments
_Ashish 14-Mar-11 14:10pm    
can you please implement your idea in your project and lets check is it really works on button click event
mmaruf 11-Sep-13 9:23am    
Thank you your comment is useful for C#
can You give me the solution in javascript?..
 
Share this answer
 
after saving complete
response.edirect("yourpage.aspx");
 
Share this answer
 
U will pass the header location and solve the problem....

header('Location:.php');
 
Share this answer
 
Comments
Richard Deeming 6-Jun-23 8:24am    
Aside from the fact that you're 12 years too late, your answer is for PHP whereas the question was for ASP.NET, and the existing solutions already mentioned redirecting the request after saving.

Stick to answering new questions unless you have something new and interesting to add to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900