Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a form that accepts information from user and stores it into database.
but problem is that when i click on refresh button or press go back on page, the data duplicates in database.

so if any one has solution please help me out..
Posted
Comments
bbirajdar 21-Jul-12 14:43pm    
How in the world the data gets duplicated in the database when you are inserting/updating the user data against the particular unique userId ?Your database design is wrong...

There is a pattern called PRG (POST - Redirect - GET). You can follow it to avoid duplicate insertions. Its simply like POST the data to the server, and redirect it to the same or different page.

There are other ways too if you want, which can be easily searched. Have a look at the discussion here[^].
 
Share this answer
 
v2
Comments
Rahul Rajat Singh 6-Jul-12 4:00am    
good answer, +5.
Pankaj Nikam 6-Jul-12 4:06am    
Thank you :)
abcforcode 9-Jul-12 9:27am    
thank you..
Pankaj Nikam 9-Jul-12 10:28am    
You are most welcome. Please mark it as answer if it helps you :)
what you can do is

1. once user hits the button, first check if the record already exist in DB(based on some key field like email ID)
2. if the check result false. insert the data in DB.
2. when user hits the refresh the check will return true let the user know that he already did it and no action was taken.

P.S. I also liked the answer by "Pankaj Nikam". that is also a good way to go.
 
Share this answer
 
Comments
Pankaj Nikam 6-Jul-12 4:08am    
Yes, even we can do it the way you mentioned :)
+5 :)
Hi,

Save your form/registration data on the Button Click Event.
or

VB
If IsPostBack Then
'' save your data to db code here
'' and reset all controls.
End If
 
Share this answer
 
v2
Comments
Pankaj Nikam 6-Jul-12 3:28am    
I believe a button click IS a post back. If you write code like the one you mentioned, it wont be saved at all.
Sanjay Kunjam 6-Jul-12 3:36am    
I think every Control in ASP.NET have their own state and value even after post_back event.
So it will be save in the Database.
I have used the same login i.e. data saving on Button Click Event.
Pankaj Nikam 6-Jul-12 4:07am    
When you click the refresh button after a post-back, you submit the data once again. Even the browser shows a warning if you try such things. Saving the data in the database should be only once and to avoid this, I have mentioned a tip in my answer.
Sanjay Kunjam 6-Jul-12 4:24am    
Good one. Thanks.

Pankaj Nikam 6-Jul-12 4:41am    
Welcome :)
Page Load()//(
{

if(!ispostback)
{
//Your code here.................
}
}
 
Share this answer
 
I think You must every event that contains code in your cs file. maybe there is some sql insertion code there.
 
Share this answer
 
 
Share this answer
 

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